Tutorials Logic, IN info@tutorialslogic.com

Inheritance in Java extends, super, Override

extends and super

Inheritance models a substitutable is-a relationship. Use overriding and super deliberately, preserve base-class contracts, and prefer composition when behavior sharing does not imply substitutability.

Before using inheritance, ask whether the child can truly stand in for the parent. If substituting the child would surprise callers, the relationship is weak and composition may create a simpler, safer design.

The extends keyword creates a subclass. The super keyword can call parent constructors and parent methods.

Basic Inheritance

Basic Inheritance
class Vehicle {
    private String brand;

    Vehicle(String brand) {
        this.brand = brand;
    }

    void start() {
        System.out.println(brand + " starting");
    }
}

class Car extends Vehicle {
    Car(String brand) {
        super(brand);
    }

    void openTrunk() {
        System.out.println("Trunk opened");
    }
}

Method Overriding

Overriding lets a subclass provide a new implementation for an inherited method. Use @Override so the compiler catches signature mistakes.

Override Example

Override Example
class Animal {
    String sound() {
        return "Some sound";
    }
}

class Dog extends Animal {
    @Override
    String sound() {
        return "Bark";
    }
}

final, protected, and Inheritance Boundaries

final prevents inheritance or overriding. protected allows access in subclasses and same-package classes. Use protected carefully because it exposes internals to subclasses.

  • Use private fields with protected methods when subclasses need controlled access.
  • Make classes final when they are not designed for extension.
  • Favor composition when the relationship is has-a instead of is-a.

Using Inheritance Carefully

Inheritance is powerful when a child really is a specialized form of the parent. If the relationship is only code reuse, composition is often cleaner because it avoids fragile parent-child coupling.

  • Use extends for is-a relationships.
  • Use super to call parent constructors or methods.
  • Do not override methods in a way that breaks parent expectations.
  • Prefer composition when behavior changes too much.
Before you move on

Inheritance in Java extends, super, Override Mastery Check

5 checks
  • The super keyword can call parent constructors and parent methods.
  • Overriding lets a subclass provide a new implementation for an inherited method.
  • Use @Override so the compiler catches signature mistakes.
  • final prevents inheritance or overriding. protected allows access in subclasses and same-package classes.
  • Use protected carefully because it exposes internals to subclasses.

Core Java Questions Learners Ask

The compiler then catches spelling and signature mistakes.

No. A subclass constructor calls a superclass constructor but does not inherit it.

Use composition when the relationship is "uses" or "has," not truly "is a."

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.