In our previous discussions, we have learnt about this keyword in Java. In this article we will see ‘What is super keyword in Java’. We will walk through all possible scenarios to use ‘super’ keyword in Java programming.
Usage of super keyword
1. super() invokes the constructor of the parent class.
2. super.variable_name refers to the variable in the parent class.
3. super.method_name refers to the method of the parent class.
Let’s discuss those things in detail
1. super() invokes the constructor of the parent class
super() will invoke the constructor of the parent class.Before getting into that we will go through the default behavior of the compiler. Even when you don’t add super() keyword the compiler will add one and will invoke the Parent Class constructor.
class ParentClass { public ParentClass() { System.out.println("Parent Class default Constructor"); } } public class SubClass extends ParentClass { public SubClass() { System.out.println("Child Class default Constructor"); } public static void main(String args[]) { SubClass s = new SubClass(); } }
Output
Parent Class default Constructor Child Class default Constructor
Even when we add explicitly also it behaves the same way as it did before.
class ParentClass { public ParentClass() { System.out.println("Parent Class default Constructor"); } } public class SubClass extends ParentClass { public SubClass() { super(); System.out.println("Child Class default Constructor"); } public static void main(String args[]) { SubClass s = new SubClass(); } }
Output
Parent Class default Constructor Child Class default Constructor
You can also call the parameterized constructor of the Parent Class. For example, super(10) will call parameterized constructor of the Parent class.
class ParentClass { public ParentClass() { System.out.println("Parent Class default Constructor called"); } public ParentClass(int val) { System.out.println("Parent Class parameterized Constructor, value: "+val); } } public class SubClass extends ParentClass { public SubClass() { super();//Has to be the first statement in the constructor System.out.println("Child Class default Constructor called"); } public SubClass(int val) { super(10); System.out.println("Child Class parameterized Constructor, value: "+val); } public static void main(String args[]) { //Calling default constructor SubClass s = new SubClass(); //Calling parameterized constructor SubClass s1 = new SubClass(10); } }
Output
Parent Class default Constructor called Child Class default Constructor called Parent Class parameterized Constructor, value: 10 Child Class parameterized Constructor, value: 10
2. super.variable_name refers to the variable in the parent class
Let’s look into the below example, here we have the same variable in both parent and subclass
class ParentClass { int val=999; } public class SubClass extends ParentClass { int val=123; public void disp() { System.out.println("Value is : "+val); } public static void main(String args[]) { SubClass s = new SubClass(); s.disp(); } }
Output
Value is : 123
This will call only the val of the sub class only. Without super keyword, you cannot call the val which is present in the Parent Class.
class ParentClass { int val=999; } public class SubClass extends ParentClass { int val=123; public void disp() { System.out.println("Value is : "+super.val); } public static void main(String args[]) { SubClass s = new SubClass(); s.disp(); } }
Output
Value is : 999
3. super.method_nae refers to the method of the parent class
When you override the Parent Class method in the Child Class without super keywords support you will not be able to call the Parent Class method. Let’s look into the below example
class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void disp() { System.out.println("Child Class method"); } public void show() { disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }
Output:
Child Class method
Here we have overridden the Parent Class disp() method in the SubClass and hence SubClass disp() method is called. If we want to call the Parent Class disp() method also means then we have to use the super keyword for it.
class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void disp() { System.out.println("Child Class method"); } public void show() { //Calling SubClass disp() method disp(); //Calling ParentClass disp() method super.disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }
Output
Child Class method Parent Class method
When there is no method overriding then by default Parent Class disp() method will be called.
class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void show() { disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }
Output
Parent Class method
Leave a Reply