Skip to content

7.1. Basics of Inheritance

Inheritance is a fundamental OOP concept that allows a class to inherit properties and behaviors from another class. It promotes code reuse and establishes "is-a" relationships.

Inheritance Syntax

java
// Base class (parent)
class Vehicle {
    protected String brand;
    protected int year;

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

    public void start() {
        System.out.println("Vehicle is starting");
    }

    public void displayInfo() {
        System.out.println("Brand: " + brand + ", Year: " + year);
    }
}

// Derived class (child)
class Car extends Vehicle {
    private int numberOfDoors;

    public Car(String brand, int year, int numberOfDoors) {
        super(brand, year);  // Call parent constructor
        this.numberOfDoors = numberOfDoors;
    }

    public void honk() {
        System.out.println("Car is honking");
    }

    @Override
    public void displayInfo() {
        super.displayInfo();  // Call parent method
        System.out.println("Doors: " + numberOfDoors);
    }
}

Types of Inheritance

Single Inheritance

java
class Animal {
    protected String name;

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

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

class Dog extends Animal {
    private String breed;

    public Dog(String name, String breed) {
        super(name);
        this.breed = breed;
    }

    public void bark() {
        System.out.println(name + " is barking");
    }
}

Multilevel Inheritance

java
class Animal {
    public void eat() {
        System.out.println("Animal is eating");
    }
}

class Mammal extends Animal {
    public void walk() {
        System.out.println("Mammal is walking");
    }
}

class Dog extends Mammal {
    public void bark() {
        System.out.println("Dog is barking");
    }
}

The super Keyword

java
class Parent {
    protected String message = "Parent message";

    public Parent(String message) {
        this.message = message;
    }

    public void display() {
        System.out.println("Parent: " + message);
    }
}

class Child extends Parent {
    private String message = "Child message";

    public Child(String parentMsg, String childMsg) {
        super(parentMsg);  // Call parent constructor
        this.message = childMsg;
    }

    @Override
    public void display() {
        super.display();  // Call parent method
        System.out.println("Child: " + message);
        System.out.println("Parent's message: " + super.message);
    }
}

Access Modifiers in Inheritance

java
class Base {
    public String publicVar = "public";
    protected String protectedVar = "protected";
    String defaultVar = "default";  // package-private
    private String privateVar = "private";  // not inherited

    public void showAccess() {
        System.out.println(publicVar);      // Accessible
        System.out.println(protectedVar);   // Accessible
        System.out.println(defaultVar);     // Accessible
        System.out.println(privateVar);     // Accessible
    }
}

class Derived extends Base {
    public void testAccess() {
        System.out.println(publicVar);      // Accessible
        System.out.println(protectedVar);   // Accessible
        System.out.println(defaultVar);     // Accessible (same package)
        // System.out.println(privateVar);  // NOT accessible
    }
}

Constructor Chaining in Inheritance

java
class A {
    public A() {
        System.out.println("A's constructor");
    }

    public A(String message) {
        System.out.println("A's constructor: " + message);
    }
}

class B extends A {
    public B() {
        // super() is called implicitly
        System.out.println("B's constructor");
    }

    public B(String message) {
        super(message);  // Explicit parent constructor call
        System.out.println("B's constructor: " + message);
    }
}

class C extends B {
    public C() {
        System.out.println("C's constructor");
    }
}

Complete Inheritance Example

java
// Base class
class Employee {
    protected String name;
    protected double baseSalary;

    public Employee(String name, double baseSalary) {
        this.name = name;
        this.baseSalary = baseSalary;
    }

    public double calculateSalary() {
        return baseSalary;
    }

    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Base Salary: $" + baseSalary);
        System.out.println("Total Salary: $" + calculateSalary());
    }
}

// Derived class
class Manager extends Employee {
    private double bonus;

    public Manager(String name, double baseSalary, double bonus) {
        super(name, baseSalary);
        this.bonus = bonus;
    }

    @Override
    public double calculateSalary() {
        return baseSalary + bonus;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("Bonus: $" + bonus);
    }

    public void conductMeeting() {
        System.out.println(name + " is conducting a meeting");
    }
}

// Usage
public class InheritanceDemo {
    public static void main(String[] args) {
        Employee emp = new Employee("John", 50000);
        Manager mgr = new Manager("Alice", 70000, 15000);

        emp.displayInfo();
        System.out.println("---");
        mgr.displayInfo();
        mgr.conductMeeting();
    }
}

Benefits of Inheritance

  1. Code Reusability: Avoid duplicating code
  2. Method Overriding: Runtime polymorphism
  3. Extensibility: Easy to add new functionality
  4. Maintainability: Changes in base class affect all derived classes

Inheritance Rules

  1. Single Inheritance: Java supports single inheritance only (one parent class)
  2. Constructor Calling: Child constructor must call parent constructor (explicitly or implicitly)
  3. Private Members: Not inherited by subclasses
  4. Access Levels: Child class cannot reduce visibility of inherited methods