Inheritance in Java

Mannan Ul Haq
0

Inheritance is a fundamental feature of object-oriented programming (OOP) in Java that allows one class to be derived from another. The class being derived from is called the parent or superclass, while the derived class is called the child or subclass. A subclass inherits all the non-private members (fields and methods) from its superclass.


Syntax:

class SubClassName extends SuperClassName {}


To illustrate inheritance, consider the following example:


class Shape
{
    protected int width;
    protected int height;

    public void setDimensions(int w, int h)
    {
        this.width = w;
        this.height = h;
    }
}

class Rectangle extends Shape
{
    public int getArea()
    {
        return this.width * this.height;
    }
}

class Main
{
    public static void main(String[] args)
    {
        Rectangle rect = new Rectangle();
        rect.setDimensions(5, 3);
        int area = rect.getArea();
        System.out.println("Area is: " + area);
    }
}


In this example, we have a super class `Shape` and a sub class `Rectangle`. The `Rectangle` class inherits from the `Shape` class. This means that all Public, Internal and Protected members of the `Shape` class are accessible in the `Rectangle` class. Private members of the base class are not accessible in the derived class.


Inheritance allows code reuse, as the sub class inherits the attributes and behaviors of the super class. It facilitates the creation of a hierarchical structure of classes, where each sub class specializes or adds functionality to the super class. This inheritance hierarchy can be extended further by creating additional sub classes from existing sub classes.


Types of Inheritance:

There are several types of inheritance that you can use to establish different relationships between the super and sub classes. Let's explore the most commonly used types of inheritance:


1. Single Inheritance:

Single inheritance is the most basic and commonly used type of inheritance. In single inheritance, a sub class inherits from a single super class.

Example:


class Super
{
    // Super class members
}

class Sub extends Super
{
    // Sub class members
}


2. Multilevel Inheritance:

Multilevel inheritance involves a chain of inheritance, where a sub class becomes the super class for another sub class. It allows the creation of a hierarchy of classes with each sub class inheriting properties and behaviors from its immediate super class.

Example:


class Super
{
    // Super class members
}

class Sub1 extends Super
{
    // Sub class 1 members
}

class Sub2 extends Sub1
{
    // Sub class 2 members
}


Constructor Call Sequence in Inheritance:

In Java, constructors of a superclass are not inherited by the subclass. However, when you create an object of a subclass, the constructor of its superclass (default constructor, if not explicitly called) gets invoked first, then the constructor of the subclass.

Let's consider an example to explain the constructor call sequence in inheritance:

class Super
{
    private int superData;

    public Super(int data)
    {
        this.superData = data;
        System.out.println("Super Constructor with data: " + superData);
    }
}

class Sub extends Super
{
    private int subData;

    public Sub(int data1, int data2)
    {
        super(data1);
        this.subData = data2;
        System.out.println("Sub Constructor with data: " + subData);
    }
}

class Main
{
    public static void main(String[] args)
    {
        Sub subObj = new Sub(10, 20);
    }
}

Output:

Base Constructor with data: 10
Derived Constructor with data: 20

The 'super' keyword is used to call the constructor of the superclass. If not provided, Java automatically inserts a call to the no-argument constructor of the superclass (super();). If the superclass does not have a no-argument constructor, you must call one of the constructors in the superclass explicitly using super.


Use of "final" Keyword in Inheritance:

In Java, the final keyword can be used to prevent a class from being subclassed. If a class is declared as final, no class can inherit from it.

final class CannotBeInherited
{
    // class members
}

// The following line would cause a compile-time error.
class Sub extends CannotBeInherited
{
    // class members
}

In the above example, Sub class cannot inherit from CannotBeInherited class as it is declared as final. This can be useful when you want to make sure that a class maintains its integrity and is not overridden or specialized in any way.

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !