Explanation of super keyword in Java

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.

Below you can see the syntax for using the "super" keyword:

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.

Here is the example of accessing by constructor

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.

Here is the example of accessing to variables of parent class

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.

Here is the example to access functions of parent class

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
As you have seen above we have accessed to parent class with super keyword.

That is all for this article.

Burak hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...