Super Keyword

The super keyword is a reference variable which refers object of the parent class

For example:

There are two class A and class B Class B extends class A

Class A{
}
Class B extends class B{

}

Then super keyword refers to the  object of class A

Lets understand Super keyword with example :

class A {
    int i = 10;
}


public class B extends A{
    void show(int i){
        System.out.println(i+"  value of current class");  //with out super kyword
        System.out.println(super.i+"  value of super class");  // with super keyword 
    }

     public static void main(String []args){
        
        B ob = new B();
        
        ob.show(20);
     }
}

In this example, there are two classes A and B.  Class B extends class A

In class A we have a variable of name i

In class B we have a method which takes argument int i and this method contain two print statement.

First print :

statement print valuable of the current class (we pass when we call the method)

Second print:

 statement print the variable of the super class

Because   if we written variable as super .variable_name then it refers to the object of the parent class



SHARE THIS PAGE





Important Link