super keyword in Java
super keyword
- The super keyword can be used to refer to the instance variable of the immediate parent class.
- The super keyword can be used to call the methods of the immediate parent class.
- The super keyword can be used to call the constructor of the immediate parent class.
i) Referring to the Instance Variables of the Immediate Parent Class
Example:
// Base class 'Vehicle'
class Vehicle
{
int maxSpeed = 120;
}
// Subclass 'Splendor' extending 'Vehicle'
class Splendor extends Vehicle
{
int maxSpeed = 150;
void show()
{
// Print 'maxSpeed' of base class, 'Vehicle'
System.out.println("Maximum Speed: " + super.maxSpeed);
}
}
public class Test
{
public static void main(String args[])
{
Splendor sp = new Splendor();
sp.show();
}
}
Output:
Maximum Speed: 120
ii) Invoking the Method of the Immediate Parent Class
Example:
// Base class 'Person'
class Person
{
void message()
{
System.out.println("This is base class method");
}
}
// Subclass 'Student'
class Student extends Person
{
void message()
{
System.out.println("This is subclass method");
}
// Note that display() is only in 'Student' class
void display()
{
// This will invoke or call current class message() method
message();
// This will invoke or call parent class message() method
super.message();
}
}
public class Test
{
public static void main(String args[])
{
Student s = new Student();
// Calling display() of Student
s.display();
}
}
Output:
This is subclass method
This is base class methodiii) Invoking the Constructor of the Immediate Parent Class
Example:
// Parent class 'Person'
class Person
{
// Default Constructor
Person()
{
System.out.println("Parent class constructor");
}
}
// Child class 'Student' extending the 'Person' class
class Student extends Person
{
Student()
{
// Invoke or call parent class constructor
super();
System.out.println("Child class constructor");
}
}
public class Test
{
public static void main(String args[])
{
Student s = new Student();
}
}
Output:
Parent class constructor
Child class constructor
Constructor Chaining in Java
In Java, constructor chaining refers to the method of calling one constructor from another constructor with respect to the current object. Inheritance can result in constructor chaining in Java. When we want to perform several tasks in a single constructor, we use constructor chaining.
Important Points about Constructor Chaining:
- A constructor call from a constructor must appear in the first step.
- The first line of the constructor is either super() or this(). By default, it is super()
- This() and super() can never exist in the same constructor.
- Constructor chaining can be accomplished in two ways: using the this() keyword and using the super() keyword.
Constructor chaining within the same class using this() keyword.
public class Student {
// Default constructor
Student() {
// Invoke parameterized constructor
this("Coding Ninjas");
System.out.println("Default constructor called");
}
// Parameterized constructor
Student(String str) {
System.out.println("Parameterized constructor called");
}
public static void main(String args[]) {
// Invoke default constructor
Student obj = new Student();
}
}
Output:
Parameterized constructor called
Default constructor called
Constructor chaining to another class using the super() keyword.
class Person {
// Person class default constructor
Person() {
// Invoke current class constructor 2
this(10, 20);
System.out.println("Person class default constructor called");
}
// Perosn class parameterized constructor
Person(int a, int b) {
System.out.println("Person class parameterized constructor called");
}
}
class Student extends Person {
// Student class default constructor
Student() {
// Invoke current class constructor
this("Coding", "Ninjas");
System.out.println("Student class default constructor called");
}
// Student class parameterized constructor
Student(String str1, String str2) {
// Invoke parent class constructor
super();
System.out.println("Student class parameterized constructor called");
}
}
public class Test {
public static void main(String args[]) {
// Calls default constructor of 'Student' class
Student obj = new Student();
}
}
Output:
Person class parameterized constructor called
Person class default constructor called
Student class parameterized constructor called
Student class default constructor called