English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented(I)

Java Object-Oriented(II)

Java Object-Oriented(III)

Java Exception Handling

Java List(List)

Java Queue(Queue)

Java Map Collections

Java Set Collections

Java Input/Output (I/O)/O)

Java Reader/Writer

Java Other Topics

Java super Keyword

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

The super keyword in Java is used in subclasses to access superclass members (properties, constructors, and methods).

Before learning about the super keyword, make sure you are familiar withJava Inheritance.

Usage of the super keyword

  1.  Call the overridden parent class's method in the subclass.

  2. If the superclass (superclass) and subclass (subclass) both have properties with the same name, then access the superclass's properties (fields).

  3. Explicitly call the superclass's no-argument constructor or parameterized constructor from the subclass constructor.

  Let's understand all these uses.

1. Access the overridden method of the superclass

If the same-named method is defined in both the superclass and subclass, the method in the subclass overrides the method in the superclass. This is calledMethod overriding.

Example1: Method overriding

class Animal {
  //Method
  public void display(){
    System.out.println("I am an animal");
  }
}
class Dog extends Animal {
  //Overriding method
  @Override
  public void display(){
    System.out.println("I am a dog");
  }
  public void printMessage(){
    display();
  }
}
class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
    dog1.printMessage();
  }
}

Output Result

I am a dog

In this example, by creating an object of the Dog class, dog1, we can call its method printMessage(), and then that method executes the display() statement.

Since display() is defined in both classes, the method in the subclass Dog overrides the method in the superclass Animal. Therefore, the subclass's display() is called.

What if you must call the overridden method of the superclass?

If you need to call the overridden method of the superclass Animal, use super.display().

Example2: super calls the superclass method

class Animal {
  //Method
  public void display(){
    System.out.println("I am an animal");
  }
}
class Dog extends Animal {
  //Overriding method
  @Override
  public void display(){
    System.out.println("I am a dog");
  }
  public void printMessage(){
    //This calls the overridden method
    display();
    // This calls the parent class's method
    super.display();
  }
}
class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
    dog1.printMessage();
  }
}

Output Result

I am a dog
I am an animal

Here is how the above program works.

2. Access the superclass's (parent) properties

Superclass and subclass can have properties with the same name. We use the super keyword to access the superclass's properties.

Example3: Access superclass properties

class Animal {
  protected String type="Animal";
}
class Dog extends Animal {
  public String type="Mammal";
  public void printType() {
    System.out.println("I am ", "" + type);
    System.out.println("I am a ", "" + super.type);
  }
}
class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
    dog1.printType();
  }
}

Output:

I am a mammal
I am an animal

In this instance, we defined the same instance field type in both the superclass Animal and the subclass Dog.
Then we created the object dog of the Dog class1. Then, call the printType() method using this object.

Inside the printType() function,

  • type  - refers to the attribute of the subclass Dog.

  • super.type  - refers to the attribute of the superclass Animal.

Therefore, System.out.println("I am " + type);Output "I am a mammal", and, System.out.println("I am a " + super.type); printOutput "I am an animal".

3. Use super() to access the superclass constructor

As is well known, the default constructor is automatically called when an object of the class is created.

To explicitly call the superclass constructor from the subclass constructor, we use super(). This is a special form of the super keyword.

Note: super() can only be used in the subclass constructor and must be the first statement.

Example4: Uses super()

class Animal {
  //Default or parameterless constructor of the Animal class
  Animal() {
    System.out.println("I am an animal");
  }
}
class Dog extends Animal {
  // Default or parameterless constructor of the Dog class
  Dog() {
    //calls the default constructor of the superclass
    super();
    System.out.println("I am a dog");
  }
}
class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
  }
}

Output Result

I am an animal
I am a dog

Here, when the object dog of the Dog class1is automatically called the default or parameterless constructor of the class when it is created.
In the subclass constructor, the super() statement calls the superclass's constructor and executes the statements within it. Therefore, the result we get"I am an animal".

Then, the program flow returns to the subclass constructor and executes the remaining statements. Therefore, the output is "I am a dog".

However, it is not necessary to use super(). Even if super() is not used in the subclass constructor, the compiler will implicitly call the superclass's default constructor.

Then, why use explicit super() and redundant code if the compiler automatically calls super()?

If it is necessary to call the superclass constructor from the subclass constructorParameterized constructor (parameterized constructor), then it must be used explicitly.

a parameterized super()must always bethe subclass constructor bodyinThe first statement, otherwise, a compilation error will occur.

Example5: Calls the parameterized constructor using super()

class Animal {
  //Default or parameterless constructor
  Animal() {
    System.out.println("I am an animal");
  }
  //Parameterized constructor
  Animal(String type) {
    System.out.println("Type: ")}+type);
  }
}
class Dog extends Animal {
  //Default Constructor
  Dog() {
    //Call the parameterized constructor of the superclass
    super("Animal");
    System.out.println("I am a dog");
  }
}
class Main {
  public static void main(String[] args) {
    Dog dog1 = new Dog();
  }
}

Output Result

Type: Animal
I am a dog

The compiler can automatically call the no-argument constructor. However, it cannot call the constructor with arguments.

If it is necessary to call the parameterized constructor, it must be explicitly defined in the subclass constructor, as shown in the statement above the code:

super("Animal");

Please note that in the example above, we used super("Animal") to explicitly call the parameterized constructor. In this case, the compiler will not call the superclass's default constructor.