Super Keyword in Java

super is a keyword in java that came in the picture at the time of inheritance and when we want to perform the operation on parent class object rather than the same class.

The keyword “super” can be used in many contexts.

1. Super keyword to call parent variable

2. Super keyword to call parent class method

3. Super keyword to call parent class constructor

super keyword to call parent variable

when a superclass and derived class has the same variable names and we want to call the value of the superclass, the super keyword can be used.

Below example shows to use of a super keyword to call parent class variable

package test;


/* Base class vehicle */
class Vehicle {
	int maxSpeed = 120;
}

/* sub class Car extending vehicle */
class Car extends Vehicle {
	int maxSpeed = 180;

	void display() {
		/* print maxSpeed of base class (vehicle) */
		System.out.println("Maximum Speed: " + super.maxSpeed);
	}
}

/* Driver program to test */
public class TestCar {
	public static void main(String[] args) {
		Car small = new Car();
		small.display();
	}
}
Example Output

super keyword to call parent class method

When a derived class wants to call an overridden method of the parent class from child class, the super keyword can be helpful.

Below example shows the use of a super keyword to call a parent class method.

package test;

/* Base class Person */
class Person {
	void message() {
		System.out.println("This is person class");
	}
}

/* Subclass Student */
class Student extends Person {
	void message() {
		System.out.println("This is student class");
	}

	// Note that display() is only in Student class
	void display() {
		// will invoke or call current class message() method
		message();

		// will invoke or call parent class message() method
		super.message();
	}
}

/* Driver program to test */
public class TestStudent {
	public static void main(String args[]) {
		Student s = new Student();

		// calling display() of Student
		s.display(); 
	}
}
Example Output

super keyword to call parent class constructor

To initialize the child class always we wanted to initialize the parent class also because of some dependencies, so while creating the object of child class we can always call the same constructor of parent class by using of super().

Here below example calls the constructor of the parent class with super() and that will be executed each time when a new object is created.

package test;

/*superclass Person*/
class Person {
	Person() {
		System.out.println("Person class Constructor");
	}
}

/* subclass Student extending the Person class */
class Student extends Person {
	Student() {
		// invoke or call parent class constructor
		super();

		System.out.println("Student class Constructor");
	}
}

/* Driver program to test */
class Test {
	public static void main(String[] args) {
		Student s = new Student();
	}
}
Example Output

Note: while calling parent class constructor from child class constructor, always remember that, this statement super() should be the first statement of constructor.

Share