java2db

How to use super keyword in java with an example



The super keyword is useful to call base class override methods,variables and  base class constructors.We can use the super keyword in a class that derived from another class ( Inheritance mechanism ).If there is no inheritance between two classes , we cannot use the super keyword.
super keyword in javasuper keyword is useful to call the base class override method like super.<base class method name>.
super keyword in javasuper keyword is useful to call the  base class override variables like  super.<variable name> .
super keyword in javaIt is useful to call the base class default constructor in derived class like  super() .
super keyword in javasuper keyword is useful to call the base class parameterized constructors like  super(argone,argtwo,….) .
super keyword in javasuper keyword will work up to one level of inheritance .
When calling the  base class constructor , the call should be in the first line of the derived class constructor.

 

 Calling base class variable  with super keyword :

Output :

 

 

 Calling  base class constructor with super keyword

 Output :

 

 

 Calling  base class override method with super keyword :

Output :

Not only the overridden methods , we can call any public,protected or default methods  from the base class using super keyword .

 

 

 

 General Example for super keyword:

Base class (circle) contains one variable called pi_value , default constructor and method circle_Area(rad).
In first line of the superDemo class constructor, we are calling the default constructor of the circle (line no 5).
Line number 8, overriding circle class variable (pi_value).
In line number 11 overriding circle class method (circle_Area()).
We are calling circle_Area() method from superDemo and overridden method circle_Area() from circle by using super keyword (in line 28 , 30) .




Related Posts :
How to work with this keyword in java
   [will not be published]



^ <