Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. extends / super / @Override

extends / super / @Override

Keywords related to class inheritance and method overriding. Use extends to inherit a parent class, super to access members of the parent class, and the @Override annotation to explicitly declare an override.

Syntax

// Define a subclass that inherits from a parent class.
class SubClass extends SuperClass {
    // Inherits the fields and methods of the parent class.
}

// Call the parent class constructor (must be the first line of the constructor).
super(args);

// Call a method of the parent class.
super.methodName(args);

// Override a method from the parent class.
@Override
returnType methodName(args) {
    // Subclass-specific implementation
}

Keywords and Syntax

KeywordDescription
extendsDefines a subclass by inheriting from a parent (super) class. Java supports single inheritance only, so you can extends only one class.
super(args)Calls the parent class constructor. Must be written as the first line of the subclass constructor.
super.methodName()Explicitly calls a method from the parent class. Allows you to reuse the parent's implementation from within an overriding method.
@OverrideTells the compiler that this method overrides a method in the parent class. Catches typos and incorrect signatures at compile time.

Sample Code

// Define the parent class.
class Animal {
    String name;

    Animal(String name) {
        this.name = name;
    }

    String speak() {
        return name + " is making a sound.";
    }
}

// Define a Dog class that extends Animal.
class Dog extends Animal {

    Dog(String name) {
        super(name); // Call the parent class constructor.
    }

    // Override speak().
    @Override
    String speak() {
        return name + " says Woof!";
    }

    // Reuse the parent class method.
    String speakTwice() {
        return super.speak() + " / " + this.speak();
    }
}

Animal a = new Animal("Animal");
Dog d = new Dog("Pochi");

System.out.println(a.speak());      // Prints "Animal is making a sound."
System.out.println(d.speak());      // Prints "Pochi says Woof!"
System.out.println(d.speakTwice()); // Prints "Animal is making a sound. / Pochi says Woof!"

// Polymorphism: a parent class variable can hold a subclass instance.
Animal a2 = new Dog("Taro");
System.out.println(a2.speak()); // Prints "Taro says Woof!"

Overview

Inheritance makes code reuse straightforward. A subclass can use the public and protected members of its parent class as-is, and can override them as needed to change their behavior.

The @Override annotation is optional, but it is strongly recommended — it lets the compiler catch method name typos and parameter type mismatches at compile time. All classes implicitly extend the Object class, so methods such as toString() and equals() are also defined in Object.

For interfaces and abstract classes, see interface / implements / abstract. For access modifiers, see public / private / protected / static / final.

If you find any errors or copyright issues, please .