SrcForge

Lesson 21

Inheritance

Inheritance lets one class acquire properties of another. Java supports Single, Multilevel and Hierarchical inheritance using the extends keyword.

Java Track

Save progress as you learn.

61 lessons

Lesson content

Inheritance allows one class to acquire properties and methods of another. It promotes reusability and reduces redundancy.

Definition List

  • Inheritance: A mechanism where one class acquires properties of another.
  • Superclass (Parent Class): The class whose members are inherited.
  • Subclass (Child Class): The class that inherits from a superclass.
  • extends Keyword: Used to establish inheritance between classes.
  • Code Reusability: Reusing existing code without rewriting it.

Java Inheritance Syntax

class subClass extends superClass {
    // methods and fields
}

Types of Inheritance in Java

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

Java does not support Multiple Inheritance through classes. It can be achieved using interfaces.

Single Inheritance in Java

One subclass inherits from one superclass.

Example 01: Single Inheritance

package Inheritance;

class Big {
    public void house() {
        System.out.println("Have Own 2BHK House.");
    }
}

class sec extends Big {
    public void car() {
        System.out.println("Have Own Audi Car.");
    }
}

public class single {
    public static void main(String args[]) {
        sec o = new sec();
        o.car();
        o.house();
    }
}

Output

Have Own Audi Car.
Have Own 2BHK House.

Multilevel Inheritance in Java

A class inherits from another class, which is itself inherited by another.

Example 02: Multilevel Inheritance

package Inheritance;

class GrandFather {
    public void house() {
        System.out.println("3 BHK House.");
    }
}

class father extends GrandFather {
    public void land() {
        System.out.println("5 Arcs of Land..");
    }
}

class son extends father {
    public void car() {
        System.out.println("Own Audi Car..");
    }
}

public class multilevel {
    public static void main(String args[]) {
        son o = new son();
        o.car();
        o.house();
        o.land();
    }
}

Output

Own Audi Car..
3 BHK House.
5 Arcs of Land..

Hierarchical Inheritance in Java

Multiple subclasses inherit from the same superclass.

Example 03: Hierarchical Inheritance

package Inheritance;

class shape {
    float length, breadth, radius;
}

class rect extends shape {
    public rect(float l, float b) {
        length = l;
        breadth = b;
    }
    float rectangle_area() {
        return length * breadth;
    }
}

class circle extends shape {
    public circle(float r) {
        radius = r;
    }
    float circle_area() {
        return 3.14f * (radius * radius);
    }
}

class square extends shape {
    public square(float l) {
        length = l;
    }
    float square_area() {
        return (length * length);
    }
}

public class Hierarchical {
    public static void main(String[] args) {
        rect o1 = new rect(2, 5);
        System.out.println("Area of Rectangle : " + o1.rectangle_area());

        circle o2 = new circle(5);
        System.out.println("Area of Circle : " + o2.circle_area());

        square o3 = new square(3);
        System.out.println("Area of Square : " + o3.square_area());
    }
}

Output

Area of Rectangle : 10.0
Area of Circle : 78.5
Area of Square : 9.0

Important Points

  • Use extends to inherit from a class.
  • Subclass accesses public and protected members.
  • Constructors are not inherited.
  • Private members are not directly accessible.
  • Java does not support multiple inheritance via classes.
  • Every Java class inherits from Object.

FAQ

1. What is inheritance in Java?

An OOP feature where one class acquires properties of another using extends.

2. How many types does Java support?

Single, Multilevel and Hierarchical through classes.

3. Why no multiple inheritance in Java?

Avoids the Diamond Problem. Use interfaces instead.

Inheritance uses the extends keyword.
Subclass inherits public and protected members.
Single: one parent, one child.
Multilevel: chain of inheritance across generations.
Hierarchical: multiple subclasses, one superclass.
Constructors and private members are not inherited.
Java avoids multiple inheritance to prevent ambiguity.

Where Java is used

Java in real-world development

Single Inheritance

One subclass inherits from one superclass. Simplest form of inheritance.

Multilevel Inheritance

Chain of classes: GrandFather → Father → Son. Each level inherits from the one above.

Hierarchical Inheritance

Multiple subclasses share one superclass. Common properties defined once.

extends Keyword

Used to create inheritance. class Child extends Parent {}

What Is Not Inherited

Constructors and private members are not inherited by subclasses.

Multiple Inheritance

Not supported via classes. Use interfaces to achieve multiple inheritance.