What is Super keyword in Java
A Super keyword in java is a keyword used to access immediate parent class instance variable, methods and constructors.
java super keyword used in classes where inheritance hierarchy exists.
Uses of super keyword in Java
In Java super keyword is used when a sub class wants to refer its immediate super (parent) class instance variable, methods and constructors then super keyword can used to access them.
- To access parent class hidden instance variable.
- To call parent class overridden method.
- To call the parent class constructor in java.
A. To access parent class hidden instance variable using super keyword.
In java programming, when a super class and sub class both has a instance variable that has a same name.
Then instance variable of sub class hides it’s super class instance variable.
This problem is known as Instance variable hiding.
We can solve the problem of Instance variable hiding, by using super keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class One { int i = 5; } class Two extends One { int i = 6; public void show() { System.out.println("Value of Two class's i=" + i); System.out.println("Value of One class's i=" + super.i); } } public class TestSuper { public static void main(String[] s) { Two two = new Two(); two.show(); } } |
Here base class and child class both contains instance variable i. Value of i is different for both classes.
Here i refers to class Two’s instance variable.
To access class one’s i here super.i
is used.
1 2 | Value of Two class's i= 6 Value of One class's i= 5 |
B. To call parent class overridden method using super keyword
In Java programming, when sub class have same method signature and return type as parent has then the method is known as overridden method.
If case of overridden method child class can not access parent class overridden method directly.
If want to access the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Shape { void area() { System.out.println("Area of Shape"); } } class Triangle extends Shape { void area() { System.out.println("Area of Triangle"); } void show() { area(); super.area(); } } public class TestShape { public static void main(String[] s) { Triangle triangle = new Triangle(); triangle.show(); } } |
1 2 | Area of Triangle Area of Shape |
Description: When triangle.show();
call area()
.
area()
calls Triangle class area().
To call Shape class area()
. Include super keyword as super.area()
C. To call the parent class constructor using super keyword in java
In java programming, to call immediate parent class constructor super() can be used.
Parent class constructor must be called from child class constructor and must be first line in constructor call.
syntax to call constructor
super()//call default constructor
super(3,3) //call two integer argument constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class A { int x, y; A(int i, int j){ // class A’s constructor x = i; y = j; System.out.println(" x =" + x + " y=" + y); } } class B extends A { int z; B(int i, int j, int k) { // class B’s constructor super(i, j); // call super class A’s constructor z = k; System.out.println(" z =" + z); } } public class Test { public static void main(String arg[]) { B obj = new B(10, 20, 30); } } |
1 2 | x =10 y=20 z =30 |
Note: Call to super class constructor must appears as the first statement with in the sub class.
Description: When super(i,j);
line will execute. super( i, j )
will call it’s immediate super class A’s constructor.
Another Example with use of constructor call using super and use of this 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 25 26 27 28 29 30 31 32 33 34 35 36 | class Person { String aadhar;//person unique number String name; int age; Person() {} Person(String aadhar, String name, int age) { this.aadhar = aadhar; this.name = name; this.age = age; } } class Student extends Person { long rollNo; String branch; String section; Student() {} Student(String aadhar, String name, int age, long rollNo, String branch, String section) { super(aadhar, name, age); this.rollNo = rollNo; this.branch = branch; this.section = section; } void display() { System.out.println("aadhar=" + aadhar + " name=" + name + " age=" + age + " rollNo=" + rollNo + " branch=" + branch + " section=" + section); } } public class TestMainStudent { public static void main(String[] args) { Student student = new Student("123212345432", "Ram", 24, 2, "CSE", "A"); student.display(); } } |
Output
1 | aadhar=123212345432 name=Ram age=24 rollNo=2 branch=CSE section=A |