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 is useful to call the base class override method like super.<base class method name>.
super keyword is useful to call the base class override variables like super.<variable name> .
It is useful to call the base class default constructor in derived class like super() .
super keyword is useful to call the base class parameterized constructors like super(argone,argtwo,….) .
super 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Author : java2db.com //To run the below example,Copy the below code and save the file with Derived_Class.java name. class BaseClass{ int value = 10; // BaseClass instance variable } //End of BaseClass public class DerivedClass extends BaseClass{ public void callValu() // Normal method in DerivedClass { System.out.println("BaseClass value is :"+super.value); } public static void main(String[] args) { // object creation for DerivedClass DerivedClass object = new DerivedClass(); // Call method using the reference object.callValu(); } } |
Output :
1 |
BaseClass value is :10 |
Calling base class constructor with super keyword
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class BaseClass{ BaseClass() // BaseClass constructor { System.out.println("Hi i am BaseClass"); } }//End of BaseClass public class DerivedClass extends BaseClass{ public DerivedClass() // Normal method in DerivedClass { super(); // BaseClass constructor call System.out.println("Hi i am DerivedClass"); } public static void main(String[] args) { // object creation for DerivedClass DerivedClass object = new DerivedClass(); }// End of main method }//End of DerivedClass |
Output :
1 2 |
Hi i am BaseClass Hi i am DerivedClass |
Calling base class override method with super keyword :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class BaseClass{ public void addTwoNumbers() { System.out.println("BaseClass sum : "+(2+3)); }// End of method }//End of BaseClass public class DerivedClass extends BaseClass{ public void addTwoNumbers() //overridden method { System.out.println("DerivedClass sum : "+(4+5)); } public void result() { super.addTwoNumbers();// Calling BaseClass overridden method addTwoNumbers(); // Calling DerivedClass method } public static void main(String[] args) { // object creation for DerivedClass DerivedClass object = new DerivedClass(); object.result(); }// End of main method }//End of DerivedClass |
Output :
1 2 |
BaseClass sum : 5 DerivedClass sum : 9 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class circle{ public float pi_value = 0; circle() { //value of pi pi_value = 22/7F; System.out.println("The pi value is "+pi_value); } public float circle_Area(float radius) { float result = pi_value*radius*radius; System.out.println("Area from the base class "+result); return result; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class superDemo extends circle{ superDemo() { super(); } //override variable public float pi_value = 0; //override method public float circle_Area(float radius) { float result = super.pi_value*radius*radius; System.out.println("Area from derived class " +result); return result; } //perimeter of the circle public float circle_perimeter(float radius) { //pi_value inherited from its base class circle float result = 4*super.pi_value*radius*radius; System.out.println("Perimeter of the circle " +result); return result; } public void resilt() { circle_Area(12.5F); //calling derived class method circle_perimeter(12.5F); super.circle_Area(13.5F);//calling base class method } public static void main(String args[]) { new superDemo().resilt(); } } |
1 2 3 4 |
The pi value is 3.142857 Area from derived class 491.0714 Perimeter of the circle 1964.2856 Area from the base class 572.7857 |
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 |