Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Using Super Keyword in Java

 

In this lesson, you will learn

  • When Constructors Are Executed?
  • Use of ‘Super’ Keyword
    • Calls Superclass Constructor
    • Access Superclass Members

 

When Constructors Are Executed?

  1. The answer is that in a class hierarchy, constructors complete their execution in order of derivation, from superclass to subclass.
  2. This order is the same whether or not super() is used.
  3. If super() is not used, then the default or parameterless constructor of each superclass will be executed.

Let’s understand using an example of when constructors are executed.

 

Example: Calling Default Constructor in Inheritance

package ch9.l5;

class A {
	A() {
		System.out.println("I am in Constructor A");
	}
}

class B extends A {
	B (){
		System.out.println("I am in Constructor B");
	}
}

class C extends B {
	C (){
		System.out.println("I am in Constructor C");
	}
}

public class CallingConstructor {

	public static void main(String[] args) {
		C objC=new C();
	}
}

Output

I am in Constructor A
I am in Constructor B
I am in Constructor C

 

Important Note!

Constructors are called in order of their derivation from superclass to subclass only in the case of the default constructor.

Questions: How to call the parameterized constructor in inheritance using subclass objects?

Answer: Using Super Keyword.

 

2. Understanding ‘Super’ Keyword

Super has the following 2 uses in inheritance.

  1. Used to call the constructor of the superclass.
  2. Used to access a member of the superclass that has been hidden by a member of a subclass.

2.1. First Use of ‘Super’ – Accessing Superclass Constructor

A subclass can call the constructor defined by its superclass by use of the following form of super

Syntax:

Here, arg-list specifies any arguments needed by the constructor in the superclass.

 

Note: super( ) must always be the first statement executed inside a subclass constructor.

Let’s understand the use of super() to call the superclass constructor through the below figure.

 

Example 1: Calling Parameterized Constructor using ‘Super’

package ch9.l5;

class First{
	int a;
	First(int a){
		this.a=a;
	}
	void displayA() {
		System.out.println("A: "+a);
	}
}

class Second extends First{
	int b;
	Second(int a, int b){
		super(a);//Calls the parameterized constructor of class 'First'
		this.b=b;
	}
	void displayB() {
		System.out.println("B: "+b);
	}
	//Calculate sum
	void output() {
		System.out.println("A+B = "+(a+b));
	}
}

public class SuperTest {

	public static void main(String[] args) {
		Second objSecond=new Second(10, 20);
		objSecond.displayA();
		objSecond.displayB();
		objSecond.output();
	}
}

Output

A: 10
B: 20
A+B = 30

 

2.2. Second Use of ‘Super’: Accessing the Superclass Member

 

 

The second form of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used.

Syntax

Here the member may be a variable or method.

 

Important Note!

This second form of super is most applicable to situations where the subclass members have the same names as its subclass. Then subclass members hide the superclass members.

 

Example 1: Accessing Superclass Variables

package ch9.l6;

//Using super to overcome name hiding.
class A {
	int i;
}

class B extends A {
	 int i; // this i hides the i in A
	 B(int a, int b) {
		 super.i = a; // i in A
		 i = b; // i in B
	 }
	 
	 void show() {
		 System.out.println("i in superclass: " + super.i);
		 System.out.println("i in subclass: " + i);
	 }
}

class UseSuper2 {
	public static void main(String args[]) {
		B subOb = new B(1, 2);
		subOb.show();
	}
}

Output

i in superclass: 1
i in subclass: 2

 

Although the instance variable i in class B hides the i in class A, super allows access to i defined in the superclass.

As you will see, super can also be used to call methods that are hidden by a subclass. That is known as Method Overriding in Java.

 

More Examples of ‘super’

 

Example 1: Using ‘super’ to Call Constructor in Singlelevel Inheritance

package ch9.l5;

class Person {
    String name;
    
    Person(String name) {
        this.name = name;
        System.out.println("Person name: " + this.name);
    }
}

class Employee extends Person {
    float salary;
    
    Employee(String name, float salary) {
        super(name); // Calls the parameterized constructor of the Person class
        this.salary = salary;
        System.out.println("Employee salary: " + this.salary);
    }
}

public class TestSuper1 {
    public static void main(String[] args) {
    	// This will initialize both the Person and 
    	// the Employee objects with given values
        new Employee("John Doe", 50000f); 
    }
}

Output

Person name: John Doe
Employee salary: 50000.0

 

Example 2: Using ‘super’ to Call Constructor in Multilevel Inheritance

package ch9.l5;

class One{
	int a;
	
	One(int a){
		this.a=a;
	}
	
	void displayA() {
		System.out.println("A: "+a);
	}
}

class Two extends One{
	int b;
	
	Two(int a, int b){
		super(a);//Calls the parameterized constructor of class 'one'
		this.b=b;
	}
	
	void displayB() {
		System.out.println("B: "+b);
	}
}

class Three extends Two{
	
	Three(int a, int b){
		super(a,b); //Calls the parameterized constructor of class 'Two'
	}
	
	//Calculate sum
	void findGreatest() {
		System.out.println("Greatest is: "+(a>b?a:b));
	}
}

public class SuperTestMultilevel {

	public static void main(String[] args) {
		Three obj=new Three(15,6);
		obj.displayA();
		obj.displayB();
		obj.findGreatest();
	}
}

Output

A: 15
B: 6
Greatest is: 15

 

Exercises

 

Exercise 1: Basic Inheritance

Objective: Create a basic inheritance structure where a subclass extends a superclass. The superclass will have a constructor that initializes a name attribute, and the subclass must call this constructor.

Superclass: Person

  • Attributes: name (String)
  • Constructor: Person(String name)

Subclass: Employee

  • Additional Attributes: employeeId (int)
  • Constructor: Employee(String name, int employeeId)

Instructions:

  1. Define the Person class with a constructor that initializes the name attribute.
  2. Define the Employee class that extends Person.
  3. In the Employee constructor, use the super keyword to call the Person constructor to initialize the name attribute, then initialize the employeeId.

 


 

Exercise 2: Multi-level Inheritance

Objective: Implement a multi-level inheritance structure where each subclass calls its immediate superclass constructor, demonstrating the chain of constructor calls.

Base Class: Vehicle

  • Attributes: brand (String)
  • Constructor: Vehicle(String brand)

Intermediate Class: Car

  • Additional Attributes: model (String)
  • Constructor: Car(String brand, String model)

Subclass: ElectricCar

  • Additional Attributes: batteryLife (int)
  • Constructor: ElectricCar(String brand, String model, int batteryLife)

Instructions:

  1. Define the Vehicle class with a constructor to initialize the brand.
  2. Define the Car class that extends Vehicle, including a constructor that calls the Vehicle constructor and initializes model.
  3. Define the ElectricCar class that extends Car, including a constructor that calls the Car constructor and initializes batteryLife.

 


 

Exercise 3: Using super with Overloaded Constructors

Objective: Create a class hierarchy where the superclass has overloaded constructors, and the subclass calls a specific superclass constructor using super.

Superclass: Book

  • Attributes: title (String), author (String)
  • Constructors:
    • Book(String title)
    • Book(String title, String author)

Subclass: EBook

  • Additional Attributes: fileSize (double)
  • Constructor: EBook(String title, String author, double fileSize)

Instructions:

  1. Define the Book class with two overloaded constructors: one for initializing title only, and another for initializing both title and author.
  2. Define the EBook class that extends Book, including a constructor that calls the Book constructor with both title and author parameters using super, and then initializes fileSize.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review