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.
class Child extends Parent { /* ... */ }A basic inheritance example
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
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
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
}
}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
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
}
}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