Java Inheritance

Inheritance is one of the four pillars of Object-Oriented Programming. It allows a new class to acquire the properties and behaviors of an existing class. The existing class is called the parent class (or superclass), and the new class is called the child class (or subclass).

Inheritance promotes code reuse — instead of rewriting common logic, a child class simply inherits it and can add or modify behavior as needed.

Real-World Analogy

Think of a Vehicle as a parent class. It has common properties like speed, fuelType, and a method move(). A Car and a Bike are specific types of vehicles — they inherit all vehicle properties and also have their own unique features.

Syntax

class ChildClass extends ParentClass {
    // additional fields and methods
}

The extends keyword establishes the inheritance relationship.

Basic Inheritance Example

class Animal {
    String name;

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

    void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Rex";

        dog.eat();    // inherited from Animal
        dog.sleep();  // inherited from Animal
        dog.bark();   // Dog's own method
    }
}

Output:

Rex is eating.
Rex is sleeping.
Rex says: Woof!

The Dog class did not define eat() or sleep() — it inherited them from Animal.

The super Keyword

The super keyword refers to the parent class. It is used to:

  • Access parent class fields that are hidden by a child class field with the same name.
  • Call the parent class constructor from the child class constructor.
  • Call a parent class method that has been overridden by the child class.

Calling Parent Constructor with super()

class Vehicle {
    String brand;
    int speed;

    Vehicle(String brand, int speed) {
        this.brand = brand;
        this.speed = speed;
    }

    void displayInfo() {
        System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String brand, int speed, int doors) {
        super(brand, speed);   // calls Vehicle constructor
        this.doors = doors;
    }

    void displayCarInfo() {
        super.displayInfo();   // calls parent method
        System.out.println("Doors: " + doors);
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("Toyota", 180, 4);
        car.displayCarInfo();
    }
}

Output:

Brand: Toyota, Speed: 180 km/h
Doors: 4

Method Overriding

A child class can provide its own implementation of a method inherited from the parent class. This is called method overriding. The overriding method must have the same name, return type, and parameters as the parent method.

class Shape {
    void draw() {
        System.out.println("Drawing a shape.");
    }
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a circle.");
    }
}

class Square extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a square.");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s1 = new Circle();
        Shape s2 = new Square();

        s1.draw();   // Drawing a circle.
        s2.draw();   // Drawing a square.
    }
}

The @Override annotation tells the compiler that the method is intentionally overriding a parent method. If the method signature does not match, the compiler will report an error.

Types of Inheritance in Java

1. Single Inheritance

One child class inherits from one parent class.

class A { }
class B extends A { }   // B inherits from A

2. Multilevel Inheritance

A class inherits from another class, which itself inherits from another class.

class Animal { }
class Mammal extends Animal { }
class Dog extends Mammal { }   // Dog → Mammal → Animal

3. Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

class Vehicle { }
class Car extends Vehicle { }
class Bike extends Vehicle { }
class Truck extends Vehicle { }

Multiple Inheritance – Not Supported for Classes

Java does not allow a class to inherit from more than one class (to avoid the "Diamond Problem"). However, multiple inheritance is supported through interfaces (covered in a later topic).

// class C extends A, B { }   // NOT ALLOWED in Java

The final Keyword and Inheritance

Using final prevents a class from being extended or a method from being overridden.

final class ImmutableClass {
    // Cannot be extended
}

class Base {
    final void display() {
        // Cannot be overridden
    }
}

Object Class – The Root of All Classes

In Java, every class implicitly extends the built-in Object class (from java.lang). This means all Java classes inherit methods like toString(), equals(), and hashCode() from Object.

Summary

  • Inheritance allows a child class to inherit fields and methods from a parent class using the extends keyword.
  • The super keyword accesses the parent class's constructor and methods.
  • Method overriding allows a child class to redefine inherited behavior.
  • Java supports single, multilevel, and hierarchical inheritance for classes.
  • Multiple inheritance via classes is not supported — use interfaces instead.
  • All Java classes ultimately extend the built-in Object class.

Leave a Comment

Your email address will not be published. Required fields are marked *