Summary: In this tutorial, you will learn about super keyword in Java with the help of the examples.

Super in Java

In Java, the super keyword refers to the immediate superclass of a class. It is used in a subclass to refer to the members of the superclass.

The super keyword is most commonly used to access members of the superclass that have been overridden by the subclass.

For example, consider the following code:

class Animal {
    public void move() {
        System.out.println("Animals can move.");
    }
}

class Dog extends Animal {
    @Override
    public void move() {
        System.out.println("Dogs can walk and run.");
    }
}

In this code, the Dog class extends the Animal class and overrides the move() method. When the move() method of the Dog class is called, it will only print “Dogs can walk and run.”, and the implementation of the move() method in the Animal class will not be executed.

To access the implementation of the move() method in the Animal class from the Dog class, you can use the super keyword, like this:

class Dog extends Animal {
    @Override
    public void move() {
        super.move();   // calls the method move() of the superclass (Animal)
        System.out.println("Dogs can walk and run.");
    }
}

Now, when the move() method of the Dog class is called, it will first print “Animals can move.”, and then “Dogs can walk and run.”

The super keyword can also be used to access other members of the superclass, such as fields and constructors. It is a useful tool for accessing the functionality of the superclass from within a subclass.

super() in Java

super can also be used to access the constructor of the superclass, using the super() syntax. This is useful if the subclass needs to initialize the members of the superclass before initializing its own members.

For example:

class Animal {
    private String name;
    public Animal(String name) {
        this.name = name;
        System.out.println("Animal constructor called.");
    }
}

class Dog extends Animal {
    private String breed;
    public Dog(String name, String breed) {
        super(name);   // calls the constructor of the superclass (Animal)
        this.breed = breed;
        System.out.println("Dog constructor called.");
    }
}

When the Dog class is instantiated, the constructors of both the Dog class and the Animal class will be called, in that order. The output will be:

Animal constructor called.
Dog constructor called.

Important Points about Super in Java

The super() call must be the first statement in the subclass constructor. If there are multiple constructors in the superclass, you can use super(parameters) to specify which constructor you want to call.

Also, super can only be used in a subclass to access members of the superclass. It cannot be used in a non-subclass or in a static context.

So, to summarize, In Java, the super keyword is used in two different ways:

  1. To refer to the instance of the superclass that is associated with a subclass object. For example: super.someField or super.someMethod()
  2. To call the superclass constructor from within a subclass constructor. For example: super()

Leave a Reply