top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

INHERITANCE IN JAVA - OBJECT ORIENTED PROGRAMMING CONCEPT

Mar 27, 2025
4 min read

Updated: Apr 2, 2025

Introduction

Inheritance is one of the fundamental principles of Object-Oriented Programming (OOP) and it plays a crucial role in Java programming. It allows one class to inherit fields and methods from another class.


Inheritance in Java

·       In Java, inheritance is a mechanism where one class acquires the fields and methods of another class. The class that inherits the properties and behaviors is called the subclass or child class, while the class being inherited from is called the superclass or parent class.

·       The main benefit of inheritance is code reusability. By inheriting methods and properties from a parent class, the child class can reuse existing functionality to avoid code duplication and improve maintainability.


How does Inheritance work in java

Inheritance is implemented using the “extends” keyword. The child class extends the parent class, which means it inherits the fields and methods from the parent class.


Types of Inheritance in Java

1.Single Inheritance

2.Multilevel Inheritance

3.Hierarchical Inheritance

4.Multiple Inheritance

5.Hybrid Inheritance


Single Inheritance

Single inheritance is the simplest form of inheritance, where a child class inherits from a single parent class.

Example of Single Inheritance


Explanation

·       The Bird class inherits the sound() method from the Animal class. This is a key feature of single inheritance, where a subclass (child class) inherits methods and properties from a single superclass (parent class).

·       The Bird class also defines its own unique behavior through the fly() method. This method is specific to the Bird class and is not inherited from the Animal class.

·       When you create an object of the Bird class (Bird b1 = new Bird();), you can invoke both the methods defined in the Bird class and the inherited methods from the Animal class.

·       b1.fly();: This calls the fly() method of the Bird class.

·       b1.sound();: This calls the sound() method inherited from the Animal class.


 Multilevel Inheritance

 In multilevel inheritance, a class is derived from another class, which is also derived from another class, creating a chain of inheritance. A grandchild class can inherit the features of a parent class and a grandparent class.


Example of Multiple Inheritance



Explanation

·       Multilevel inheritance occurs when a class inherits from another class, which itself inherits from another class, creating a chain of inheritance. In this case, the Puppy class inherits from Dog, and Dog inherits from Animals.

·       A subclass Puppy inherits all the methods from its parent class Dog and grandparent class Animals. This means Puppy can access the bark() method from Dog and the eat() method from Animals even though they are not explicitly defined in Puppy.

·       Multilevel inheritance helps reuse code. For example, the eat() method in Animals can be used by both Dog and Puppy without rewriting it in each class, making the code more efficient and easier to maintain.


Hierarchical Inheritance

Hierarchical inheritance is a type of inheritance where multiple classes inherit from a single parent class. In this inheritance model, one base (or parent) class is shared by multiple subclasses, allowing them to reuse the functionality defined in the parent class.

Example of Hierarchical Inheritance


Explanation

·       In hierarchical inheritance, multiple classes Dog, Puppy, and Cat inherit from a single parent class Animal. This allows the child classes to share common functionality eat() while adding their own unique functionality  bark(), weeps(), and meow().

·       Both Dog and Puppy share the eat() method because they inherit it from Animal. Similarly, Puppy and Cat can both access the eat() method from Animal. This eliminates the need to redefine the same method in each class, promoting code reusability.

 

Multiple Inheritance

·       Multiple inheritance refers to the concept where a class can inherit properties and methods from more than one parent class. but Java does not support multiple inheritance directly through classes to avoid ambiguity and complexity.

·       Multiple inheritance can only be achieved through interfaces. That is, a class can implement multiple interfaces, but it can only extend one class.

·       Java does not support multiple inheritance due to the diamond problem. The diamond problem occurs when a class inherits from two classes that have a method with the same signature. This creates ambiguity about which method should be called, leading to confusion in method resolution.

 

Example of Multiple Inheritance


 Explanation

  ·       The Animalss interface has a method eat(), and the Flyinganimal interface has a method fly(). A class bat implements both of these interfaces, meaning the bat class must provide implementations for both eat() and fly() methods.

·       The bat class implements both interfaces by overriding their methods. In the eat() method and in the fly() method.

·       In the main method, an object of bat is created (b1). The methods eat() and fly() are called on the object b1, which results in the respective print statements being executed, demonstrating the behavior of the bat as defined by the class.

 

Hybrid Inheritance

Hybrid inheritance is a combination of more than one types of inheritance. Hybrid inheritance is when a class gets methods from both a class and an interface. In Java, you can extend (inherit) from one class and implement (inherit) from multiple interfaces.

Example of Hybrid Inheritance


Explanation

 ·       The Animal class has two methods eat() and sleep(), which define general behavior for all animals. There are also two interfaces, FlyingAnimal and SwimmingAnimal, each defining one method (fly() and swim(), respectively). These interfaces specify that any class implementing them must provide its own implementations for these methods.

·       The Duck class extends the Animal class and implements both the FlyingAnimal and SwimmingAnimal interfaces. It provides implementations for the fly() and swim() methods. Thus, the Duck class can both fly and swim in addition to the behaviors inherited from the Animal class, such as eating and sleeping.

·       In the main method, an object of the Duck class is created. The methods eat(), sleep(), fly(), and swim() are called on this object. The calls to eat() and sleep() invoke methods from the Animal class, while the calls to fly() and swim() invoke methods from the implemented interfaces. This demonstrates the concept of hybrid inheritance in Java, where a class can inherit from a superclass (in this case, Animal) and also implement multiple interfaces (FlyingAnimal and SwimmingAnimal).


Conclusion

In conclusion, inheritance is a key concept in object-oriented programming that enables code reuse, organization, and the creation of relationships between classes. By using different types of inheritance—single, multilevel, hierarchical, multiple (through interfaces) and hybrid—one can build flexible and efficient systems.


Happy Learning!













































 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page