How to use super keyword in Java
In Java, super
is used to refer to the parent class constructor, variables or method. In other words, when we have a method in the derived class that overrides the method of the base class then we can use the super keyword to call the method of base class. Similarly, we can use super keyword when we need to access the instance variable or constructor of the base class from the derived class.
The examples given below demonstrate all the uses of the super in Java.
Accessing Instance variables:
In the following example, we have a variable called workinghrs
for both base and derived classes. Now let’s try to access the base class workinghrs
variable from the derived class using the super keyword.
// Base class Staff
class Staff
{
int workinghrs = 50;
}
// Derived class Teaching extending Staff
class Teaching extends Staff
{
int workinghrs = 40;
void display()
{
// print workinghrs of base class (Staff)
System.out.println("Maximum Working hrs: " + super.workinghrs);
// Working hours of derived class
System.out.println("Working hrs for teaching staff : " + workinghrs);
}
}
public class Main
{
public static void main(String[] args)
{
Teaching t = new Teaching();
t.display();
}
}
Output:
Maximum Working hrs: 50
Working hrs for teaching staff : 40
Calling the Constructor:
Just like variables, we can also access the parent class constructor from the child class.
In the following example, Staff
is a parent class and Teaching
is a child class, so we are going to see how to call the Staff constructor from the Teaching constructor using super keyword.
// Base class Staff
class Staff
{
int workinghrs;
Staff()
{
workinghrs=50;
}
}
// Derived class Teaching extending Staff
class Teaching extends Staff
{
int workinghrs;
int diff;
Teaching(int h)
{
super();
workinghrs=h;
diff=super.workinghrs-workinghrs;
}
void display()
{
// print workinghrs of base class (Staff)
System.out.println("The teaching staff works for "+diff+" hours less than the other staff");
}
}
public class Main
{
public static void main(String[] args)
{
Teaching t = new Teaching(40);
t.display();
}
}
Output:
The teaching staff works for 10 hours less than the other staff
Note: The most important thing that we have to keep in mind while calling the base class constructor from the derived class is, the invocation of the super should be the first statement in the child class constructor.
Calling the method:
We can also access the parent class methods from the child class using super
keyword, In the following example, we are going to call display() method of base class from the derived class using super keyword.
// Base class Staff
class Staff
{
String name;
String department;
Staff(String n, String d)
{
name=n;
department=d;
}
void display()
{
System.out.println("Name "+name);
System.out.println("Department "+department);
}
}
// Derived class Teaching extending Staff
class Teaching extends Staff
{
int numberofsubjects;
String specialization;
Teaching(String nm, String d, int n, String s)
{
// Calling Constructor of Staff class
super(nm,d);
numberofsubjects=n;
specialization=s;
}
void display()
{
// Calling method of Staff class
super.display();
System.out.println("No of subjects "+numberofsubjects);
System.out.println("Specialization "+specialization);
}
}
public class Main
{
public static void main(String[] args)
{
Teaching t = new Teaching("John","Computer",2,"Data Science");
t.display();
}
}
Output:
Name John
Department Computer
No of subjects 2
Specialization Data Science
Conclusion:
Hence, super
keyword is used to access the super class or base class variables, constructor, and method from the derived class.
References:
Happy Learning 🙂