Java super keyword with Examples & Uses

This tutorial explains about super keyword in java which is quite used in java programs. It covers details like what super keyword is, uses of super keyword, difference between this and super keyword etc. Before you start this tutorial, you should have knowledge about inheritance in java, if not then refer inheritance in java tutorial first.

What is super keyword in Java

In java, super is a keyword which is used by subclasses to access the instance variable, method and constructor of immediate superclass. The keyword super itself suggests that it's a reference variable of superclass. It is similar as this keyword except that it is used by subclasses to access the superclass members.

What is immediate superclass ?

If class C extends class B, class B extends class A, then for class C immediate superclass will be class B and for class B immediate superclass will be class A.

The keyword super can be used only inside instance method, constructor and block of subclass. Like other non static members, it can not be used inside static method or static block, doing so will result in compilation error. The syntax of using super keyword is :

 super.name // To access the name instance variable of parent class 
 super.printDetail() // To call the printDetail method of parent class
 super() // To call no-argument constructor of parent class

Using super keyword does not create an object of superclass, rather the current object is viewed as an instance of the superclass of current class. Using super keyword in subclass is same as using this keyword inside superclass.

Can we access private members of superclass inside subclass using super keyword ?

No, using super keyword we can access only those method, constructor or variable of superclass inside subclass which is accessible as per access modifier. Private members are accessible only within class, not outside the class.

Super keyword program in Java

  class A {  
     String str = "Class A string variable";
     void message() {
      System.out.println("Inside class A");      
     }  
  }
  class B extends A {   
     String str = "Class B string variable";
     void print() {
       super.message();
       System.out.println(super.str); 
       System.out.println(this.str); 
    }   
    public static void main(String [] args) {
      B obj = new B();  
      obj.print();      
    }
 }

Output:

Inside class A
Class A string variable
Class B string variable

Is it possible to assign reference to super keyword ?

No, doing(super = someObjName) so will result in compilation error.

Different uses of super keyword in Java

The super keyword is used to access superclass instance variable, method and constructor inside subclass instance method constructor or block. To list down some of use cases :

  • If a subclass has an instance variable with same name as superclass variable, then super keyword can be used to access the superclass variable inside subclass.
  • If subclass has overridden any superclass method, then super keyword can be used to access the overridden method of superclass inside subclass.
  • The subclass constructors can call superclass constructor using super keyword. If a subclass constructor needs to call superclass constructor, then superclass constructor call must be the first statement inside the subclass constructor.
  • We can also call any normal instance method(not overridden) or variable of superclass using super keyword.

Java program of super keyword uses inside method and constructor

 class Person {  
    String name;
    String msg;
    
    public Person(String name, String msg) {
      this.name = name;
      this.msg = msg;
     } 
    void message() {
      System.out.println("Inside message method of Person class");      
     }
   }
 class Employee extends Person { 
    String msg;  
    int empId;   
    public Employee(String name, int id) {
      super(name,"Variable of Person class"); // Superclass constructor call must be first statement
      this.empId = id;
      this.msg = "Variable of Employee class";
     }
    void printDetail() {
      System.out.println("Name = "+this.name+", empId = "+this.empId);
      super.message();
      this.message();
      System.out.println(super.msg); 
      System.out.println(this.msg); 
     }
    void message() {
      System.out.println("Inside message method of Employee class");    
     }
    public static void main(String [] args) {
      Employee emp = new Employee("Rahul", 20);  
      emp.printDetail(); 
     }
  }

Output:

Name = Rahul, empId = 20
Inside message method of Person class
Inside message method of Employee class
Variable of Person class
Variable of Employee class

Note : We can not use super and this keyword together for calling constructor inside a subclass constructor. For example using super() and this() inside a subclass constructor will result in compilation error.

Can we use super keyword to access static method or variable ?

Yes we can, but accessing static members using super keyword is not a good programming style.

Can we use super keyword in a class which doesn't extend any other class ?

Yes we can, since in java every class internally extends Object class if it doesn't extend any other class.

Does java implicitly invokes superclass no-arg constructor ?

Yes if a subclass constructor does not explicitly invoke a superclass constructor or another subclass constructor, the java compiler implicitly inserts a call to the no-argument constructor of the superclass. In this case if the superclass does not have a no-argument constructor, you will get a compile-time error.

  class A {  
    public A() { 
      System.out.println("Class A constructor");
    }
  }
  class B extends A {  
    public B() { 
      System.out.println("Class B constructor");
    }
    public static void main(String [] args) {
       B ob = new B();   
    }
  }

Output:

Class A constructor
Class B constructor

Here the object creation of subclass using new keyword invokes it's constructor which implicitly invokes the superclass constructor as well. After compilation the subclass constructor would be something like this :

 public B() { 
   super(); Compiler implicitly inserts call to the no-argument constructor of superclass
   System.out.println("Class B constructor");
 }

How super keyword is different from normal reference variable

  • We can not assign any reference(object) to super keyword explicitly while a normal reference variable can refer to any other reference variable of same type.
  • A normal reference variable can be created directly inside a class but super keyword can not be used outside of instance method, constructor or block.
  • We can not use super keyword inside static block or method but we can create normal reference variable inside static block/method and then we can use that reference variable.
  • The keyword super does not create a new object, instead the current object is viewed as an instance of superclass. While a normal object must be created first before it can be used.

Should I prefer to use super keyword over normal object of superclass in subclasses ?

Yes you should prefer to use super keyword rather than creating an object of superclass to access superclass members. Creating a normal object takes extra space in memory.

Difference between this and super keyword in java

There are not much difference between this and super keyword, both keywords are very similar in terms of functionality except their purpose of use. To list down some of the differences :

  • The keyword this is used to refer current class object while super keyword is used to refer current object but viewed as an instance of the superclass of the current class.
  • The keyword this is used to access instance variables, methods and constructor of current class while super keyword is used to access instance variables, methods and constructors of superclass.
  • If subclass needs to call parent class constructor using super keyword explicitly, then calling superclass constructor must be the first statement inside the subclass constructor.
  • To invoke an overridden method of superclass inside a subclass we use super keyword while to invoke overriding method we use this keyword or normal object of subclass.
  • Using this keyword you can call any method of current class but super keyword can call only those methods which are accessible as per access modifiers in subclass.
★★★
  • The super keyword can not be passed as an argument inside a method call.
  • The super keyword can not be used to return superclass object from a method.
  • We can not do super.super in java programs, doing so will result in compilation error.
  • Parent class constructor is always called before the child class constructor. This is also called constructor chaining.
  • As superclass constructors are not inherited in subclass, so the only way to call superclass constructor in subclasses is to use super keyword.