What is an inheritance in Java - onlyxcodes

Thursday 18 July 2024

What is an inheritance in Java

In this tutorial, I will show you what inheritance is in Java and the types of inheritance in Java with examples.


One of the key concepts of object-oriented programming (OOP) is inheritance. It enables the inheritance of behaviors and attributes (fields and methods) from an existing class to a new class. The class inherited from is referred to as the superclass or base class, while the class inherited from another class is referred to as the subclass or derived class.


what is an inheritance in java

Table Content

1. Why Use Inheritance?

2. Types of Inheritance in Java

3. Advantages of Inheritance

4. Disadvantages of Inheritance

5. Conclusion:


1. Why Use Inheritance?

Reusability: By encouraging the reuse of already-written code, inheritance reduces redundancy.


Method Overriding: Specific implementations of methods that are already defined in their superclasses can be provided by subclasses.


Extensibility: It is possible to incorporate new behaviors and features into old code without changing it.


Polymorphism: Subclasses provide simpler and generalized code because they can be viewed as instances of their superclass.


2. Types of Inheritance in Java

Java supports the following types of inheritance:


1. Single Inheritance


2. Multilevel Inheritance


3. Hierarchical Inheritance


4. Multiple Inheritance (Through Interfaces)


1. Single Inheritance


A class inherits from only one superclass when it uses a single inheritance.


Example:


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

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

public class Test
{
    public static void main(String[] args) 
	{
        Dog obj = new Dog();
        obj.eat(); // Inherited from Animal
        obj.bark(); // Defined in Dog
    }
}

Output:


This animal eats food.
The dog barks.

2. Multilevel Inheritance


A class is derived from another class, which is derived from still another class, and so on, creating a chain of multilevel inheritance.


Example:


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

class Mammal extends Animal {
    void giveBirth() {
        System.out.println("This mammal gives birth to live young.");
    }
}

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

public class Test 
{
    public static void main(String[] args) 
	{
        Dog obj = new Dog();
        obj.eat(); // Inherited from Animal
        obj.giveBirth(); // Inherited from Mammal
        obj.bark(); // Defined in Dog
    }
}

Output:


This animal eats food.
This mammal gives birth to live young.
The dog barks.

3. Hierarchical Inheritance


Several classes inherit from a single superclass via hierarchical inheritance.


Example:


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

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

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

public class Test
{
    public static void main(String[] args) 
	{
        Dog obj = new Dog();
        obj.eat(); // Inherited from Animal
        obj.bark(); // Defined in Dog

        Cat obj1 = new Cat();
        obj1.eat(); // Inherited from Animal
        obj1.meow(); // Defined in Cat
    }
}

Output:


This animal eats food.
The dog barks.
This animal eats food.
The cat meows.

4. Multiple Inheritance (Through Interfaces)


Java does not directly enable multiple inheritance to keep things clear and simple. Interfaces, however, can be used to accomplish it. A class can inherit from several sources by implementing numerous interfaces.


Example:


interface Animal {
    void eat();
}

interface Pet {
    void play();
}

class Dog implements Animal, Pet {
    public void eat() {
        System.out.println("The dog eats.");
    }

    public void play() {
        System.out.println("The dog plays.");
    }
}

public class Test
{
    public static void main(String[] args) 
	{
        Dog obj = new Dog();
        obj.eat(); // Implemented from Animal
        obj.play(); // Implemented from Pet
    }
}

Output:


The dog eats.
The dog plays.

3. Advantages of Inheritance

Code Reusability: Code can be reused thanks to inheritance, which can cut down on redundancy and increase maintainability.


Method Overriding: permits particular implementations of methods defined in a superclass to be provided by subclasses.


Polymorphism: Gives an action the ability to respond differently depending on what it is acting upon.


Extensibility: It is simple to add new features to classes that already exist without having to change them.


4. Disadvantages of Inheritance

Tight Coupling: Because subclasses rely on the superclass, the system may be more closely connected and difficult to modify.


Complexity: Deep inheritance hierarchies can be difficult to understand and keep up with.


Fragile Base Class Problem: Subclasses may potentially be impacted by changes made to the superclass.


5. Conclusion:

Java's robust inheritance capability allows for polymorphism, flexibility, and code reuse. Code that is more effective and manageable can result from comprehending and applying inheritance correctly. To minimize potential challenges like tight coupling and additional complexity, it should be used carefully.

No comments:

Post a Comment

Post Bottom Ad