What is super keyword in java ?

Variables and methods of super class can be overridden in subclass. In case of overriding, a subclass object call its own variables and methods. Subclass cannot access the variables and methods of super-class because the overridden variables or methods hides the methods and variables of super class.

But still java provides a way to access super class members even if its members are overridden. Super is used to access super-class variables, methods, constructors.

Super can be used in three forms :

  1. First form is for calling super class constructor.
  2. Second one is to call super class variables.
  3. Third one is to call super class methods.

Example (calling super class constructor):-

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

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

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

/* Driver program to test*/class Test {
    public static void main(String[] args) {
        Car c = new Car();
    }
}

Output:-

Vehicle class Constructor
Car class Constructor

 

Example (call super class variables):-

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

/* 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 */class Test {
    public static void main(String[] args) {
        Car car = new Car();
        car.display();
    }
}

Output:-

Maximum Speed: 100

 

Example (call super class methods):-

/* Base class Vehicle */class Vehicle {
    void show() {
        System.out.println("This is Vehicle class");
    }
}

/* Subclass Car */class Car extends Vehicle {
    void show() {
        System.out.println("This is Car class");
    }

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

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

/* Driver program to test */class Test {
    public static void main(String args[]) {
        Car c = new Car();

        // calling show() of Car 
        c.display();
    }
}

Output:-

This is Vehicle class
This is Car class

 

Recent Posts

Explain Platform?

Any hardware or software environment in which a program runs, is known as a platform.…

5 years ago

What is Multi-threading in Java?

Multi-threading is a Java feature that allows concurrent execution of two or more parts of…

5 years ago

What is ‘IS-A’ relationship in java?

'is a' relationship is also known as inheritance. We can implement 'is a' relationship or…

5 years ago

Explain Java Coding Standards for Constants?

Constants in java are created using static and final keywords. Constants contains only uppercase letters.…

5 years ago

Explain Java Coding Standards for variables ?

Variable names should start with small letters. Variable names should be nouns. Short meaningful names…

5 years ago

Explain Java Coding standards for Methods?

Method names should start with small letters. Method names are usually verbs If method contains…

5 years ago