Mastering Java Inheritance: Concepts, Types, and Practical Examples
Mastering Java Inheritance
Explore Java inheritance in depth—understand its principles, real‑world examples, and how to apply it for cleaner, reusable code.
Inheritance is a cornerstone of object‑oriented programming that lets you create a new class based on an existing one.
The derived class is called a subclass (child or derived), while the class it extends is the superclass (parent or base). The extends keyword performs this relationship.
class Animal {
// methods and fields
}
class Dog extends Animal {
// Dog inherits fields and methods from Animal
}
Here, Dog inherits all members of Animal—the subclass gains the parent’s functionality.
Example 1: Basic Inheritance
class Animal {
String name;
public void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
public void display() {
System.out.println("My name is " + name);
}
}
class Main {
public static void main(String[] args) {
Dog labrador = new Dog();
labrador.name = "Rohu";
labrador.display();
labrador.eat();
}
}
Output
My name is Rohu I can eat
In this example, Dog inherits name and eat() from Animal, so a Dog object can access them directly.

The is‑a Relationship
Inheritance in Java represents an is‑a relationship: a subclass is a specialized form of its superclass.
- Car is a Vehicle
- Orange is a Fruit
- Surgeon is a Doctor
- Dog is an Animal
These natural hierarchies justify the use of inheritance.
Method Overriding
When a subclass defines a method with the same signature as its superclass, the subclass’s method overrides the superclass’s version. This allows runtime polymorphism.
Example 2: Overriding eat()
class Animal {
public void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
@Override
public void eat() {
System.out.println("I eat dog food");
}
public void bark() {
System.out.println("I can bark");
}
}
class Main {
public static void main(String[] args) {
Dog labrador = new Dog();
labrador.eat();
labrador.bark();
}
}
Output
I eat dog food I can bark
The eat() call on labrador resolves to the overridden method in Dog.
Tip: The @Override annotation is optional but highly recommended—it ensures the compiler checks that you are indeed overriding a method.
Using super to Access the Superclass
When overriding, the super keyword lets you invoke the parent’s implementation.
Example 3: super in eat()
class Animal {
public void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
@Override
public void eat() {
super.eat();
System.out.println("I eat dog food");
}
public void bark() {
System.out.println("I can bark");
}
}
class Main {
public static void main(String[] args) {
Dog labrador = new Dog();
labrador.eat();
labrador.bark();
}
}
Output
I can eat I eat dog food I can bark
Here, super.eat() calls the method defined in Animal before executing the subclass’s logic.
Similarly, super can invoke superclass constructors.
Protected Members in Inheritance
Fields or methods marked protected are accessible to subclasses regardless of package.
Example 4: Accessing Protected Members
class Animal {
protected String name;
protected void display() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
public void getInfo() {
System.out.println("My name is " + name);
}
}
class Main {
public static void main(String[] args) {
Dog labrador = new Dog();
labrador.name = "Rocky";
labrador.display();
labrador.getInfo();
}
}
Output
I am an animal. My name is Rocky
The subclass can freely read and modify the protected field name and call display().
Why Use Inheritance?
- Code Reuse: A child class automatically inherits all public and protected members of its parent.
- Polymorphism: Overriding enables dynamic method dispatch, a key feature of runtime polymorphism.
Types of Inheritance
Java supports five conceptual inheritance models—each useful in different scenarios.
1. Single Inheritance
A class extends only one superclass.

2. Multilevel Inheritance
A subclass becomes the superclass of another class, creating a chain.

3. Hierarchical Inheritance
Multiple subclasses inherit from a single superclass.

4. Multiple Inheritance
Java does not allow a class to extend more than one class, but interfaces provide a way to achieve this pattern.

Note: Multiple inheritance can be implemented via interfaces; see Java implements multiple inheritance.
5. Hybrid Inheritance
A blend of two or more inheritance types—for example, combining hierarchical and multiple inheritance.

Java
- Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
- Mastering Java Reflection: Inspecting Classes, Methods, and Fields at Runtime
- Mastering Java’s ObjectInputStream: A Comprehensive Guide
- Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
- Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
- Mastering Java Reader Class: Subclasses, Methods, and Practical Example
- Mastering Java Generics – Building Reusable, Type‑Safe Code
- Mastering Java File Operations with java.io – Creation, Reading, Writing & Deletion
- Mastering Java Inheritance: Subclassing and Superclass Principles
- Mastering C# Inheritance: Build Reusable, Maintainable Code