Super keyword :

The super keyword in java is used in three ways :- 

  • super can be used to refer immediate parent class instance variable.
  • super can be used to invoke immediate parent class method.
  • super() can be used to invoke immediate parent class constructor.
  • super can be used to refer immediate parent class instance variable.

E.g.

class SuperClass

{

          int num=10;

}

class SubClass extends SuperClass

{   

          int num=20;

          void display()

         {

                System.out.println(num);

               System.out.println(super.num);

         }

public static void main (String [] args)

     {

     SubClass s= new SubClass();

     s.display();

      }

}

—————————————————————————————————————————————-

Output : 20

                  10

————————————————————————————————————————————— 

In the above example, SuperClass and SubClass both classes have a common variable num. If we print variable without super keyword, it will print the variable num of current class by default. To access the parent num variable, we need to use super keyword.

 

super can be used to invoke immediate parent class method

 E.g.

class SuperClass

{

            void run()

            {

            System.out.println(“parent run method”);

            }

}

class SubClass extends SuperClass

{

   void run()

            {

               System.out.println(“child run method”);

             }

   void display()

           {

               run();

               super.run();

           }

public static void main (String [] args)

    {

     SubClass s = new SubClass();

     s.display();

    }

}

————————————————————————————————————————————— 

Output : child run method

                  parent run method

—————————————————————————————————————————————-

In the above example, SuperClass and SubClass both classes have a method with the same method signature(name) run(). If we print invoke method without super keyword, it will invoke the run() method of current class(SubClass) by default. To access the parent class i.e. SuperClass run() method, we need to use super keyword.

 

super() can be used to invoke immediate parent class constructor

E.g :

 

class SuperClass

{

            SuperClass()

            {

            System.out.println(“parent class contructor”);

            }

}

class SubClass extends SuperClass

{

   SubClass()

           {

               Super();

               System.out.println(“child child class constructor”);  

             }

  public static void main (String [] args)

     {

            SubClass s = new SubClass();

    }

}

—————————————————————————————————————————————-

Output : parent class constructor

                 child class constructor

 —————————————————————————————————————————————

 In the above example the ParentcClass constructor is invoked in the SubClass constructor using super().If we do not write a constructor, a default constructor is provided by the JVM and that default constructor also contains super() as its first statement.

 

Leave a Reply