Java super Keyword
Java super Keyword In Java, the keyword ‘super’ and ‘this’ are very similar. The super keyword is a reference variable that refers to the parent class object. When we talk about super keyword, the Java inheritance comes in our mind, so to understand super keyword in java, we should have understood the concept of Java inheritance. It is used to access the superclass methods and constructor. It can pass the value from derived class constructor to base class constructor or data members. The super() should be the first statement of derived class constructor. Super cannot call the private methods or objects of the parent class; it can only call the public and protected. It can’t be used in static methods.
Uses of Java Super keyword
Super can be used in the following context.- Use of super with variables
- Use of super with methods
- Use of super with constructors
Variable hiding by super keyword
If instance variable name is same in both superclass and sub-class, then sub-class instance variable hides the superclass variable. Let's have a look at the following example to understand the technique of variable hiding. Examplepublic class SuperClass { int est =1996; String compName = "Amazon.com"; } public class SubClass extends SuperClass { int est = 1977; String compName = "oracle.com"; void compDetail() { System.out.println("Company establish in : " + est); System.out.println("Company Name is : " + compName); } public static void main(String[] args) { SubClass s= new SubClass(); s.compDetail(); } }Output
Company establish in : 1977 Company Name is : oracle.comThere are two instance variables, est, compName in both superclass and sub-classes. When we access the variables from displayCompdetail() method, then it prints the current class variable because the current class variable hides the superclass instance variable, which is having the same name. Now, see the same example with the use of the super keyword.
public class SuperClass { int est =1996; String compName = "Amazon.com"; } public class SubClass extends SuperClass { int est = 1977; String compName = "oracle.com"; void displayCompDetail() { System.out.println("Company establish in : " + super.est); System.out.println("Company Name is : " + super.compName); } public static void main(String[] args) { SubClass s= new SubClass(); s.displayCompDetail(); } }Output
Company establish in : 1996 Company Name is : Amazon.comNow we can see in the given an example, we have used the super keyword then it calls the superclass variables and hides the sub-class variable.
Use of super with variables
When a child class and a parent class have same data member then we use the super keyword with variables to identify them. Super keyword also reduces the ambiguity because of variable. Let’s have a look at the following example.class X{ String color= "red";} class Y extends X{ String color="black"; void printColor() { System.out.println(color); System.out.println(super.color); } } class TestSuper1{ public static void main(String args[]){ Y obj= new Y(); obj.printColor(); } }Output-
black redIn this example, when we have used the super keyword then it refers super class object and when we haven’t used super then it refers the current class object. Example 2
class ParentClass { int n = 50; } class ChildClass extends ParentClass { int n = 80; void printNumber(){ System.out.println(super.n); } public static void main(String args[]){ ChildClass obj= new ChildClass(); obj.printNumber(); } }Output
50In the given example, when we use the super keyword then it refers the parent class object.
Use of super with methods
When a parent and child class have the same method, then we use the super keyword with methods to identify them. This process is called method overriding. In the case of method overriding, we can use the keyword super along with the method to implement the method of the parent class. Let's have a look at the below example Example 1class ParentClass { void display(){ System.out.println("Parent class method"); } } class Child_class extends ParentClass { void display(){ System.out.println("Child class method"); } void printMsg(){ display(); super.display(); } public static void main(String args[]){ Child_class obj= new Child_class(); obj.printMsg(); }}Output
Child class method Parent class methodExample 2 Another example will help you to understand the usage of super keyword with method.
class Car{ void speed() { System.out.println("speed is 120 km/h");} } class Alto extends Car{ void average() { System.out.println("40 km/ltr"); } void work(){ super.speed(); average(); } } class TestSuper2{ public static void main(String args[]){ Alto d=new Alto(); d.work(); }}Output-
speed is 120 km/h 40 km/ltrUse of super with constructor In the case of a constructor, super is also used to access the parent class constructor. Super can call parametric as well as a non-parametric constructor. Let's have a look at the following example- Example 1 Let's understand how super keyword acts with parent class constructor.
class ParentClass { ParentClass() { System.out.println("Parent class Constructor"); } } class ChildClass extends ParentClass { ChildClass() { super(); //invoke super class constructor System.out.println("Child class Constructor"); } } class SuperDemo { public static void main(String[] args) { ChildClass c = new ChildClass(); } }Output
Parent class Constructor Child class ConstructorThis is an example of use of super keyword with constructor. Example 2
class Vehicle{ Vehicle() { System.out.println("Vehicle has Designed");} } class Model extends Vehicle{ Model() { super(); System.out.println("Latest model has launched"); } } class TestSuper3{ public static void main(String args[]){ Model m=new Model(); }}Output-
Vehicle has Designed Latest model has launched