Skip to content
UPEEC

Inheritance in Java

extends, method overriding and super in Java.

Easy
Comments 0
class Motor {
    void start() { System.out.println("Motor starting"); }
}

class StepperMotor extends Motor {
    @Override
    void start() {
        super.start();
        System.out.println("Energising coils");
    }
}

Java allows single inheritance of classes (one parent) but a class may implement many interfaces. Every class ultimately extends java.lang.Object. final prevents overriding or subclassing.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.