Easy Tutorial
For Competitive Exams

Java Programming Super Keyword

What is Super Keyword in Java?

The super keyword in java is a reference variable that is used to refer parent class objects.

The keyword “super” came into the picture with the concept of Inheritance.

How to use super with variable?

This scenario occurs when a derived class and base class has same data members.

In that case there is a possibility of ambiguity for the JVM.

Syntax

super.<variable_name>;

Example:

class Parentclass
{
 int num=100;
}

class Subclass extends Parentclass
{
 int num=110;
 void printNumber(){
 System.out.println(super.num);
 }
 public static void main(String args[]){ 
 Subclass obj= new Subclass();
 obj.printNumber();	
 }
}
Try it Yourself

How to use super with method?

This is used when we want to call parent class method.

So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword.

Syntax

super.<method_name> ;

Example:

class Parentclass
{
 void display(){
System.out.println("Parent class method");
 }
}
class Subclass extends Parentclass
{
void display(){
System.out.println("Child class method");
 }
 void printMsg(){
display();
super.display();
 }
 public static void main(String args[]){	
Subclass obj= new Subclass();
obj.printMsg(); 
 }
}
Try it Yourself

If there is no method overriding then we do not need to use super to call the parent class method.

Example:

class Parentclass
{
 void display(){
System.out.println("Parent class method");
 }
}
class Subclass extends Parentclass
{
 void printMsg(){

System.out.println("child class method");
 } 
 public static void main(String args[]){

Subclass obj= new Subclass();
 obj.display();
 obj.printMsg(); 
 }
}
Try it Yourself

How to use super with constructor?

super keyword can also be used to access the parent class constructor.

When we create the object of sub class, first the constructor of parent class gets invoked and then the constructor of child class.

It happens because compiler itself adds super() (it invokes parent class constructor) to the constructor of child class.

Syntax

super();
super(args);

Without calling super() explicitly

class Parentclass
{
 Parentclass(){
 System.out.println("Constructor of Superclass");
 }
}
class Subclass extends Parentclass
{
 Subclass(){

System.out.println("Constructor of Subclass");
 }
  
 public static void main(String args[]){

Subclass obj= new Subclass();
 
 }
}
Try it Yourself

We can call super() explicitly too

class Parentclass
{
 Parentclass(){
System.out.println("Constructor of Superclass");
 }
}
class Subclass extends Parentclass
{
 Subclass(){
super();
System.out.println("Constructor of Subclass");

 }
  public static void main(String args[]){	
Subclass obj= new Subclass();
  
 }
}
Try it Yourself

Note:

  • super() must be the first statement in constructor.
  • We can also invoke parameterized constructor of parent class by providing arguments while calling super.

Solve this!!

14586.What is the output of this program?
    class A {
        public int i;
        private int j;
    }    
    class B extends A {
        void display() {
            super.j = super.i + 1;
            System.out.println(super.i + " " + super.j);
        }
    }    
    class inheritance {
        public static void main(String args[]){
            B obj = new B();
            obj.i=1;
            obj.j=2;   
            obj.display();     
        }
   }
1
2
1 2
Error
Explanation:
class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
14587.What is the output of this program?
    class A {
        public int i;
        public int j;
        A() {
            i = 1;
            j = 2;
	}
    }    
    class B extends A {
        int a;
	B() {
            super();
        } 
    }    
    class super_use {
        public static void main(String args[]){
            B obj = new B();
            System.out.println(obj.i + " " + obj.j)     
        }
   }
1
2
1 2
Error
Share with Friends