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 classsuper.printDetail()// To call the printDetail method of parent classsuper()// 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
classA { String str ="Class A string variable";voidmessage() { System.out.println("Inside class A"); } }classBextendsA { String str ="Class B string variable";voidprint() {super.message(); System.out.println(super.str); System.out.println(this.str); }public static voidmain(String [] args) { B obj =newB(); 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
superkeyword can be used to access the superclass variable inside subclass. - If subclass has overridden any superclass method, then
superkeyword can be used to access the overridden method of superclass inside subclass. - The subclass constructors can call superclass constructor using
superkeyword. 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
superkeyword.
Java program of super keyword uses inside method and constructor
classPerson {Stringname;Stringmsg;publicPerson(String name, String msg) {.name = name;this.msg = msg; }thisvoidmessage() { System.out.println("Inside message method of Person class"); } }classEmployeeextendsPerson {Stringmsg;intempId;publicEmployee(String name,intid) {super(name,"Variable of Person class");// Superclass constructor call must be first statement.empId = id;this.msg =this"Variable of Employee class"; }voidprintDetail() { System.out.println("Name = "+this.name+", empId = "+this.empId);super.message();this.message(); System.out.println(super.msg); System.out.println(this.msg); }voidmessage() { System.out.println("Inside message method of Employee class"); }public static voidmain(String [] args) { Employee emp =newEmployee("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.
classA {publicA() { System.out.println("Class A constructor"); } }classBextendsA {publicB() { System.out.println("Class B constructor"); }public static voidmain(String [] args) { B ob =newB(); } }
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 :
publicB() {super();Compiler implicitly inserts call to the no-argument constructor of superclassSystem.out.println("Class B constructor"); }
How super keyword is different from normal reference variable
- We can not assign any reference(object) to
superkeyword 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
superkeyword can not be used outside of instance method, constructor or block. - We can not use
superkeyword 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
superdoes 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
thisis used to refer current class object whilesuperkeyword is used to refer current object but viewed as an instance of the superclass of the current class. - The keyword
thisis used to access instance variables, methods and constructor of current class whilesuperkeyword is used to access instance variables, methods and constructors of superclass. - If subclass needs to call parent class constructor using
superkeyword 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
superkeyword while to invoke overriding method we usethiskeyword or normal object of subclass. - Using
thiskeyword you can call any method of current class butsuperkeyword 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.


