Super keyword is used for differentiating the Base/Parent class features with Derived/Child class features .

Super keyword plays very important role at three places :

1) at variable level

2) at method level

3) at Constructor level

  • 1) Super at variable level

    Whenever we inherit the Base/Parent class members into Derived/Child class, there is a possibility that Parent class members are similar to Child class members.

    In order to distinguish the Parent class members with Child class members , in the Child class , the Parent class members will be preceded by a super keyword .

    Syntax for super at Variable Level :

    super. Parent class member name


    Create Parent Class.

    
    class Parent {
    	int a;
    }
    


    Create Child class.

    
    class Child extends Parent {
    	int a;
    
    	Child(int a, int b) {
    		super.a = a;// super refer intermediate parent object
    		a = b;// by default 'a' is preceded with 'this.' since 'this.'  represents current class
    	}
    
    	public void show() {
    		System.out.println("value of Parent a =" + super.a);
    		System.out.println("value of Child a =" + a);// we can also use this.a
    	}
    
    	public void sum() {
    		System.out.println("sum =" + (super.a + a));
    
    	}
    
    	public static void main(String arr[]) {
    		Child c = new Child(5, 8);
    		c.show();
                c.sum();
    
    	}
    }

    Output:
    value of Parent a =5
    value of Child a =8
    sum =13

    Execute Child class because it contain main method.

  • 2) Super at method level

    Whenever we inherit the Parent class methods into the Child class, there is a possibility that Parent class methods are similar to Child methods.

    In order to differentiate the Parent class methods with Child class methods in the Child class, the Parent class methods must be preceded by a super keyword .

    Syntax for super at method level

    super. Parent class method name


    Create Parent class

    
    class Parent {
    	int a;
        public void show()
        {
            System.out.println("a ="+a);
        }
        
    }


    Create Child class.

    
    class Child extends Parent {
    	int a;
    
    	Child(int a, int b) {
    		super.a = a;// super refer intermediate parent object
    		this.a = b;// by default 'a' is preceded with 'this.' since 'this.'
    					// represents current class
    	}
    
    	public void show() {
    		System.out.println("value of Parent a =" + super.a);
    		System.out.println("value of Child a =" + a);// we can also use this.a
    	}
    
    	public void sum() {
            //calling show of parent 
            super.show();//referring parent class method
            this.show();//referring child class method
    		System.out.println("sum =" + (super.a + a));
    
    	}
    
    	public static void main(String arr[]) {
    		Child c = new Child(5, 8);		
            c.sum();
    
    	}
    }

    Output:
    a =5
    value of Parent a =5
    value of Child a =8
    sum =13

    Execute Child class because it contain main method.

  • 3) Super at Constructor Level

    super( arg if any ) is used to call intermediate parent class constructor from child class constructor.

    Example)
    drag_handle
     
    class Parent {
    	Parent() {
    		System.out.println("This is parent class Constructor...");
    		System.out.println("Instantaiting parent");
    	}
    
    	public void sayHelloParent() {
    		System.out.println("Hello parent");
    	}
    }
    
    class Child extends Parent {
    	Child() {
    		super();// using super to call parent default constructor
    		System.out.println("This is child class constructor");
    		System.out.println("Instantiating child");
    	}
    
    	public void sayHello() {
    		System.out.println("Hello welcome here");
    	}
    }
    
    class Executor {
    	public static void main(String arr[]) {
    		Child c = new Child();
    		c.sayHelloParent();
    		c.sayHello();
    	}
    }
    Output:
    This is parent class Constructor...
    Instantaiting parent
    This is child class constructor
    Instantiating child
    Hello parent
    Hello welcome here

    super () is used for calling super class default constructor from default constructor or from parameterized constructor of derived class. It is optional . if we don't write super() in child class constructor then JVM add it automatically for us.

    Super (....) is used for calling super class parameterized constructor either from default constructor or from parameterized constructor of derived class. It is always mandatory.

    Rule :Whenever we use either super () or super (argument) in child class constructors they must be used as first statement in child class constructor otherwise we will get error .


    case 1 : Whenever we want to call constructor of Parent class from the default constructor of Child class using super () then super() in Child class is optional . we can skip that. For example:

    
    class Parent {
    	Parent() {
    		System.out.println("This is parent class constructor...");
    	}
    }
    
    class Child1 extends Parent {
    	Child1() {
    		// we have not used any super() here
    		System.out.println("This is child1 class construtor....");
    	}
    }
    
    class Child2 extends Child1 {
    	Child2() {
    		super();// optional
    		System.out.println("This is child2 class constructor ");
    	}
    }
    
    class Executor {
    	public static void main(String arr[]) {
    		Child2 c = new Child2();// create child2 object
    	}
    }

    Output:
    This is parent class constructor...
    This is child1 class construtor....
    This is child2 class constructor

    case 2 :Whenever we want to call parameterized constructor of Parent class from the constructor of Child class using super(argument) then it is mandatory for us to write super(argument) funtion in child class constructor .
    Note : actual and formal parameters must be same.
    for example :

    
    class Parent {
    	int a;
    
    	Parent(int a) {
    		System.out.println("This is parent class Parameterized constructor...");
    		this.a = a;
    	}
    
    	public void show() {
    		System.out.println("Value of a =" + this.a);
    	}
    }
    
    class Child1 extends Parent {
    	Child1(int a) {
    		super(a);// calling parent class parameterized constructor
    		System.out.println("This is child1 class parameterized construtor....");
    	}
    }
    
    class Child2 extends Child1 {
    	Child2(int a) {
    		super(a);
    		System.out.println("This is child2  class parameterized constructor ");
    	}
    }
    
    class Executor {
    	public static void main(String arr[]) {
    		Child2 c = new Child2(560);// create child2 object
    		c.show();
    	}
    

    Output:
    This is parent class Parameterized constructor...
    This is child1 class parameterized construtor....
    This is child2  class parameterized constructor
    Value of a =560

    if We skip super(..) in this case we will get compile time error.


Example of super keyword.(real life example)
drag_handle

class Person {
	String name;
	String address;

	Person(String name, String address) {
		this.name = name;
		this.address = address;
	}
}

class Employee extends Person {
	double salary;

	Employee(String name, String address, double salary) {
		super(name, address);// calling parent constructor
		this.salary = salary;
	}

	public void showDetail() {
		System.out.println("Name : " + super.name);
		System.out.println("Address : " + super.address);
		System.out.println("Salary : " + this.salary);

	}
}

class Executor {
	public static void main(String arr[]) {
		Employee e = new Employee("John ", "Gomti Nagar", 145441.36);
		e.showDetail();
	}

}

Output:

Name : John
Address : Gomti Nagar
Salary : 145441.36


Example of super keyword. Sum of number.
drag_handle

class Sum2 {
	int a;
	int b;

	Sum2(int a, int b) {
		this.a = a;
		this.b = b;
	}

	public int getSum2() {
		int c = this.a + this.b;
		System.out.println("sum of a and b is " + c);
		return c;
	}

}

class Sum3 extends Sum2 {
	int c;

	Sum3(int a, int b, int c) {
		super(a, b);
		this.c = c;
	}

	public int getSum3() {
		int c = super.a + super.b + this.c;
		System.out.println("sum of a,b and c is " + c);
		return c;

	}
}

class Executor {
	public static void main(String arr[]) {
		Sum3 s = new Sum3(2, 5, 4);
		s.getSum3();
		s.getSum2();

	}
}

Output:

sum of a,b and c is 11
sum of a and b is 7

What next?

The next topic is final keyword in java

Share this page