Super keyword in java

 

Super keyword in java

Super keyword in java with Example

The super keyword is a reference variable which refers to its parent class, which means inheritance must be there. Same as this keyword in java, which refers to the same class object. We can say that whenever the base class version of the method is called which is overridden in the child class. Here super can access the immediate parent class method and variables.


For example, consider the following program

class Parent

{

     void display()

      {

                System.out.println (“I am from parent class”);

      }

}

class Child extends Parent

{

       void display()

      {

                display();     // it lead to recursion

                System.out.println (“I am from Child class”);

      }

}

When we display() method in child class lead to recursion.

public class RunPrg

{

    public static void main(String args[])

    {

        Child ob=new Child();

        ob.display ();

    }

}

To avoid recursion we need a super keyword for calling the parent class method. In the following child class version, we used the super in display() method.

public class Child extends Parent

{

    void display()

      {

        super.display();   // use of super to call the parent class method

                System.out.println ("I am from Child class");

      }

}

the super keyword can be used to refer parent class instance variable.

The instance variable of the parent class can be accessed in the child class with the help of the super keyword.

public class Parent

{   

    String team="KKR";    

}

After the parent class, we define the child class, which calls the parent class instance variable with the super keyword.

public class Child extends Parent

{

    String team="RCB";

    void display()

      {

        System.out.println ("Team of Parent=" + super.team);

                System.out.println ("Team of Child =" + team);

      }

}

We need to run the child with RunPrg class where the main() method defined.

public class RunPrg

{

    public static void main(String args[])

    {

        Child ob=new Child();

        ob.display();

    }

}

Output

Team of Parent=KKR

Team of Child =RCB

 

the super keyword can be used to refer parent class constructor

with the help of the super keyword, we can access the parent class constructor as follows

public class Parent

{   

    Parent()

    {

        System.out.println("I am parent class constructor");

    }

}

Note: Call to super() must be first the statement in the child (drive) Class constructor.

Below is a child class example

public class Child extends Parent

{

    Child()

    {

        super(); // called the parent class constructor

        System.out.println("I am Child class constructor");

    }

}

And then run the program with another class as follows

public class RunPrg

{

    public static void main(String args[])

    {

        Child ob=new Child();       

    }

}

Output will as

I am parent class constructor

I am Child class constructor


More Java program


SHARE THIS
Previous Post
Next Post