Single Inheritance in Java with Example

Inheritance is one of the key features of object-oriented programming (OOP).Single Inheritance enables a derived class(Sub class) to inherit properties and behavior from a single parent class(Super class)

Flow Diagram

The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class.

Example of Single Inheritance

Below code represents Single Inheritance in Java, where we can see the Rectangle class is inheriting only one parent class(Shape class).

package com.javainterviewpoint.inheritance;
public class Shape 
    int length;
    int breadth;
public class Rectangle extends Shape
    int area;
    public void calcualteArea()
        area = length*breadth;
    public static void main(String args[])
        Rectangle r = new Rectangle();
        //Assigning values to Shape class attributes
        r.length = 10;
        r.breadth = 20;
        //Calculate the area
        System.out.println("The Area of rectangle of length \""
                +r.length+"\" and breadth \""+r.breadth+"\" is \""+r.area+"\"");

Output :

The Area of rectangle of length "10" and breadth "20" is "200"

Leave a Reply

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