Explanation of super keyword in Java
Hello everyone, In this article we are going to talk about super keyword in Java. We will access parent class from child classes by using super keyword in Java.Let's get started...
Java is an object-oriented programming language that the objects are the key to everything. Objects have variables (known as instance variables) and methods with it to store and handle the data. Sometimes, we need to access the variables or methods of the parent class of an object from the subclass. The super keyword in Java programming language helps us to access to member properties of parent class.
What is the super keyword in Java?
The super keyword in Java is used to refer to the superclass of the current object. It is used to access the variables and methods of the superclass from the subclass. By using the super keyword, we can call the constructors of the superclass and also access the instance variables of the superclass.
super.member_name;
Here, "member_name" can be a variable, method, or constructor of the parent class.
Now lets take a look at usages of super keyword with examples.
Call the constructor of the superclass
When a subclass is derived, the constructor of the superclass is called first, and then the constructor of the subclass is called. Sometimes we need to call the constructor of the superclass explicitly from the constructor of the subclass. In such cases, we can use the super keyword to call the constructor of the superclass.
class Vehicle {
public Vehicle() {
System.out.println("Vehicle constructor method is called");
}
}
class Dog extends Vehicle {
public Car() {
super(); //calling the constructor of the superclass
System.out.println("Car constructor method is called");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
}
}
Output will be like below
Vehicle constructor method is called
Car constructor method is called
Access the variables of the parent class
If the subclass and the superclass have the same instance variable, then the instance variable of the subclass will be accessed by default. If we want to access the instance variable of the superclass, we can use the super keyword.
class Vehicle {
String color = "brown";
}
class Car extends Vehicle {
String color = "black";
public void displayColor() {
System.out.println("Car color is " + color);
System.out.println("Vehicle color is " + super.color); // accessing the instance variable of the superclass
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.displayColor();
}
}
Example output will be like below
Car color is black
Vehicle color is brown
Accessing to methods of the parent class
If the subclass and the superclass have the same method, then the method of the subclass will be called by default. If we want to call the method of the superclass, we can use the super keyword.
class Vehicle {
public void greet() {
System.out.println("Vehicle");
}
}
class Car extends Vehicle {
public void greet() {
super.greet(); // calling the display method of the superclass
System.out.println("Car");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.greet();
}
}
Program output will be like below
Vehicle
Car
That is all for this article.
Burak hamdi TUFAN
Comments