Lesson 38 of 50 – Java Inheritance
76%

Java Inheritance

Inheritance is an important concept in Object-Oriented Programming (OOP). It allows one class to inherit accessible fields and methods from another class, helping developers reuse and extend existing code.

1. What Is Inheritance?

Inheritance allows a new class to extend an existing class. The existing class is called the parent class or superclass, and the new class is called the child class or subclass.

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

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

    public static void main(String[] args) {
        Dog d1 = new Dog();

        d1.eat();
        d1.bark();
    }
}
Output:
Animal is eating
Dog is barking

The Dog class inherits the accessible eat() method from Animal.

2. Why Do We Use Inheritance?
  • To reuse existing class functionality.
  • To reduce duplicate code.
  • To extend a class with additional features.
  • To support runtime polymorphism through overriding.
  • To organize related classes into a hierarchy.

For example, several vehicle classes may share common features:

class Vehicle {
    void start() {
        System.out.println("Vehicle started");
    }
}

class Car extends Vehicle {
    void drive() {
        System.out.println("Car is driving");
    }
}

The Car class can use the inherited start() method and define its own drive() method.

3. Syntax of Inheritance

The basic syntax uses the extends keyword:

class Parent {
    // Parent members
}

class Child extends Parent {
    // Additional child members
}

Example:

class Person {
    String name = "Rahul";
}

class Student extends Person {
    int age = 20;

    void display() {
        System.out.println(name);
        System.out.println(age);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.display();
    }
}
Output:
Rahul
20
4. Single Inheritance

Single inheritance occurs when one child class extends one parent class.

class Animal {
    void eat() {
        System.out.println("Eating food");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }

    public static void main(String[] args) {
        Dog d1 = new Dog();

        d1.eat();
        d1.bark();
    }
}
Output:
Eating food
Barking

Dog inherits from one direct superclass, Animal.

5. Multilevel Inheritance

Multilevel inheritance occurs when a class extends another child class, creating a chain of inheritance.

class Animal {
    void eat() {
        System.out.println("Animal eats");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

class Puppy extends Dog {
    void weep() {
        System.out.println("Puppy weeps");
    }

    public static void main(String[] args) {
        Puppy p1 = new Puppy();

        p1.eat();
        p1.bark();
        p1.weep();
    }
}
Output:
Animal eats
Dog barks
Puppy weeps

Puppy inherits accessible members through the Dog and Animal classes.

6. Hierarchical Inheritance

Hierarchical inheritance occurs when multiple child classes extend the same parent class.

class Animal {
    void eat() {
        System.out.println("Animal eats");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("Cat meows");
    }
}

class Main {
    public static void main(String[] args) {
        Dog d1 = new Dog();
        Cat c1 = new Cat();

        d1.eat();
        d1.bark();

        c1.eat();
        c1.meow();
    }
}
Output:
Animal eats
Dog barks
Animal eats
Cat meows
7. Multiple Inheritance in Java

Java does not allow a class to extend multiple classes. However, a class can implement multiple interfaces.

Invalid class inheritance:

// Invalid Java syntax
// class C extends A, B {
// }

Multiple inheritance through interfaces:

interface Printable {
    void print();
}

interface Showable {
    void show();
}

class Demo implements Printable, Showable {

    public void print() {
        System.out.println("Printing");
    }

    public void show() {
        System.out.println("Showing");
    }

    public static void main(String[] args) {
        Demo d1 = new Demo();

        d1.print();
        d1.show();
    }
}
Output:
Printing
Showing
A Java class can extend only one direct class, but it can implement multiple interfaces.
8. The super Keyword

The super keyword can be used to access accessible superclass members and invoke a superclass constructor.

class Animal {
    String name = "Animal";

    void display() {
        System.out.println("Parent method");
    }
}

class Dog extends Animal {
    String name = "Dog";

    void show() {
        System.out.println(super.name);
        super.display();
    }

    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.show();
    }
}
Output:
Animal
Parent method

Here, super.name accesses the accessible superclass field, and super.display() invokes the superclass method.

9. Method Overriding in Inheritance

Method overriding occurs when a subclass provides its own implementation of an inherited instance method.

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }

    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.sound();
    }
}
Output:
Dog barks

The overridden method in Dog runs when d1.sound() is called.

10. Constructors in Inheritance

Constructors are not inherited. However, creating a child object involves invoking a superclass constructor before the subclass constructor completes.

class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child constructor");
    }

    public static void main(String[] args) {
        Child c1 = new Child();
    }
}
Output:
Parent constructor
Child constructor

The child constructor implicitly invokes the superclass's no-argument constructor in this example.

11. Advantages and Limitations

Advantages:

  • Reuse accessible parent-class functionality.
  • Reduce duplicated code.
  • Extend existing classes with additional features.
  • Support runtime polymorphism through overriding.

Limitations and considerations:

  • Java classes cannot extend multiple classes.
  • Private superclass members cannot be accessed directly by subclasses.
  • Constructors are not inherited.
  • Deep inheritance hierarchies can make programs harder to maintain.
  • Inheritance should represent a meaningful relationship between classes.
12. Real-Life Example: Employee Management
class Employee {
    String name;
    double salary;

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

    void display() {
        System.out.println("Name: " + name);
        System.out.println("Salary: " + salary);
    }
}

class Manager extends Employee {
    String department;

    Manager(String name, double salary, String department) {
        super(name, salary);
        this.department = department;
    }

    @Override
    void display() {
        super.display();
        System.out.println("Department: " + department);
    }
}

class Main {
    public static void main(String[] args) {
        Manager m1 = new Manager(
            "Amit", 50000, "IT"
        );

        m1.display();
    }
}
Output:
Name: Amit
Salary: 50000.0
Department: IT

Manager inherits the name, salary, and display functionality from Employee, then adds a department and overrides the display method.

13. Practice Assignments
  1. Create a Person class and a Student class that extends Person.
  2. Create Animal and Dog classes to demonstrate single inheritance.
  3. Create Animal, Dog, and Puppy classes to demonstrate multilevel inheritance.
  4. Create Animal, Dog, and Cat classes to demonstrate hierarchical inheritance.
  5. Create two interfaces and implement both in one Java class.
  6. Demonstrate method overriding using a Vehicle and Car class.
  7. Use super() to call a parent constructor.
  8. Create an Employee and Manager program with inherited fields and an overridden method.
Practice Tip: Run each program separately and observe which methods are inherited, overridden, or added by the child class.

🧠 Quick Quiz

1. Which keyword is used to inherit a class?




2. Can a Java class extend two classes directly?




3. Which keyword can invoke a superclass constructor?