Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

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.

Mastering Java Inheritance: Concepts, Types, and Practical Examples

The is‑a Relationship

Inheritance in Java represents an is‑a relationship: a subclass is a specialized form of its superclass.

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?


Types of Inheritance

Java supports five conceptual inheritance models—each useful in different scenarios.

1. Single Inheritance

A class extends only one superclass.

Mastering Java Inheritance: Concepts, Types, and Practical Examples

2. Multilevel Inheritance

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

Mastering Java Inheritance: Concepts, Types, and Practical Examples

3. Hierarchical Inheritance

Multiple subclasses inherit from a single superclass.

Mastering Java Inheritance: Concepts, Types, and Practical Examples

4. Multiple Inheritance

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

Mastering Java Inheritance: Concepts, Types, and Practical Examples

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.

Mastering Java Inheritance: Concepts, Types, and Practical Examples

Java

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