JavaInheritance

Inheritance

What Is Inheritance?

Inheritance lets one class acquire the fields and methods of another class, so that shared behavior only needs to be written once. The class being inherited from is the superclass (or parent class), and the class doing the inheriting is the subclass (or child class). In Java, this relationship is declared with the extends keyword.

Java
class Child extends Parent { /* ... */ }

A basic inheritance example

Java
public class Animal {
    String name;

    void eat() {
        System.out.println(name + " is eating");
    }
}

public class Dog extends Animal { // Dog inherits from Animal
    void bark() {
        System.out.println(name + " says Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.name = "Rex";  // inherited field
        myDog.eat();         // inherited method
        myDog.bark();        // Dog's own method
    }
}
Rex is eating
Rex says Woof!
What Gets Inherited

A subclass automatically gains access to the public and protected members (fields and methods) of its superclass, as if it had declared them itself. Private members of the superclass exist in every subclass object too, but they are not directly accessible by name from the subclass's own code — only through public or protected methods the superclass provides.

  • public members — inherited and accessible everywhere

  • protected members — inherited and accessible within the subclass and same package

  • package-private (no modifier) members — inherited only if the subclass is in the same package

  • private members — not directly accessible from the subclass, even though they still exist in the object

Single Inheritance for Classes
Warning
Unlike some languages that allow a class to extend multiple parent classes at once, a Java class can extend only ONE other class. This is called single inheritance, and it's a deliberate design choice that avoids the ambiguity problems (like the classic "diamond problem") that multiple class inheritance can create.

Java still gives you a way to combine behavior from multiple sources: interfaces. A class can implement any number of interfaces, which is how Java achieves multiple-inheritance-like flexibility without the pitfalls of inheriting implementation from several classes at once. Interfaces are covered on their own dedicated page.

Constructor Chaining with super(...)

When a subclass object is created, its superclass's constructor must run first to properly initialize the inherited part of the object. Java does this automatically, but you can also call a specific parent constructor explicitly with super(...).

A peek at super(...) — covered fully on its own page

Java
public class Animal {
    String name;

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

public class Dog extends Animal {
    Dog(String name) {
        super(name); // calls Animal's constructor
    }
}
Note
The super keyword deserves its own deep dive — calling parent constructors, invoking overridden parent methods, and accessing shadowed parent fields are all covered in detail on the super-keyword page.
Every Class Inherits from Object

If a class doesn't explicitly extend anything, it implicitly extends java.lang.Object. This means every class in Java — including Animal above — automatically inherits methods like toString(), equals(), and hashCode(), even without writing extends Object yourself.

Object methods are available even without an explicit extends

Java
public class Widget { } // implicitly extends Object

public class Main {
    public static void main(String[] args) {
        Widget w = new Widget();
        System.out.println(w.toString()); // inherited from Object
    }
}
Note
The Object class and the behavior of its inherited methods — especially why you'll usually want to override toString() and equals() yourself — is covered on the dedicated Object class page.
Why Use Inheritance?
  • Code reuse — shared fields and behavior are written once in the superclass

  • Establishing an "is-a" relationship (a Dog is an Animal), which enables polymorphism

  • Organizing related classes into a hierarchy that mirrors the real-world relationships they model

  • Use inheritance for genuine is-a relationships; prefer composition ("has-a") when a class merely needs to use another class's functionality