DEV Community

Dinesh G
Dinesh G

Posted on

Inheritances in Java

What is inheritances ?

An object of one class acting as an object of another class.

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented Programming system.

[ Extends keyword -> child to parent ]

=> is a Relationship
=> has a Relationship

Subclass

The class that extends the features of another class is known as child class, subclass or derived class.

Superclass

The class whose properties and functionalities are used (inherited) by another class is known as parent class, superclass or Base class.

Advantages of Inheritance

  1. Inheritance enables overriding, allowing the subclass to provide a meaningful implementation of a superclass method.

  2. It promotes reusability by allowing the subclass to use the methods and fields defined in the parent class.

  3. When changes are needed, updating the code in the superclass automatically reflects across all subclasses, simplifying maintenance.

  4. Inheritance helps avoid code duplication by placing common logic in the superclass and sharing it across multiple classes.

Syntax:

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

class Child extends Parent {
    void show() {
        System.out.println(" child class.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)