Java super keyword

    super keyword

    In Java, super is a reference variable that refers to the immediate parent class. An instance of the superclass will get created implicitly whenever we create an instance of its subclass. You can then inherit the superclass and can access the data members of the superclass using the super keyword.

    Super keyword usage-

    • It will allow you to refer to the immediate parent class instance variable .
    • It will allow you to invoke the immediate parent class method.
    • It will allow you to invoke the immediate parent constructor.

    Referring to the parent’s class instance variable

    This keyword will allow solving the ambiguity when the superclass and the subclass both have the same data member. You can access the data member or the fields of the superclass using the super keyword.

    Example-

    class Animal{  
        String color="Black";  
    }  
    
    class Dog extends Animal{  
        String color="Beige";  
        
        void Color(){  
           System.out.println(color);
           System.out.println(super.color);
        }  
    
    }  
    
    class Demo{  
         public static void main(String args[]){  
            Dog s=new Dog();  
            s.printColor();  
         }
    }

    Output-

    Beige
    Black

    In the above example, both the classes have the same data member as color. By default, the value of the current class data member will get displayed but if you want to display the value of the parent class then the super keyword will be used.

    Invoking parent’s class method

    You can invoke the parents class method using the super keyword. It is used in the case that both the parent class and the child class have the method of the same name. It means if the method is overridden you can access the parent’s class method using super keyword.

    Example-

    class Animal{  
        void drink(){System.out.println("drinking");}  
    }  
    
    class Dog extends Animal{  
          void drink(){System.out.println("eating bread");}  
    
          void run(){System.out.println("running");}  
    
          void demo_function(){  
               super.drink();  
               run();  
          }  
    }  
    
    class Demo{  
           public static void main(String args[]){  
               Dog d=new Dog();  
               d.demo_function();  
           }
    }

    Output-

    drinking 
    running 

    Invoking parent’s class constructor

    You can invoke the parent’s class constructor using the super keyword.

    Example-

    class Animal{  
        Animal(){System.out.println("animal class");}  
    }  
    
    class Dog extends Animal{  
          Dog(){  
             super();  
             System.out.println("dog class");  
          }  
    }  
    
    class Demo{  
          public static void main(String args[]){  
             Dog d=new Dog();  
          }
    }  

    Output-

    animal class
    dog class

    Super is implemented by the  compiler explicitly

    The super keyword is added by the compiler automatically while creating the instance of the child class. First, the parent’s class constructor will get invoked then the child class constructor will get executed.

    Example-

    class Animal{  
         Animal(){System.out.println("animal class");}  
    }  
    
    class Dog extends Animal{  
          Dog(){  
               System.out.println("dog class");  
          }  
    }  
    
    class Demo{  
           public static void main(String args[]){  
               Dog d=new Dog();  
           }
    }  

    Output-

    animal class
    dog class