Inheritance in OOP: Reusing Code and Creating Class Hierarchies

Inheritance in OOP

  • In Java, inheritance in OOP permits one class (the subclass) to acquire the properties and behaviors (fields and methods) from another class (the superclass) with the help of extends keyword.
  • This component supports the concept of “reusability” and “various leveled classification.”
  • There are a few types of inheritance in Java, each advertising special ways to structure the class relationships.
  • Inheritance in OOP is also known as IS-A relationship.

Extends Keyword

  • It is used to connect two classes together where first class represent new property and second class represent old property.
Inheritance in OOP
Inheritance in OOP

Types of Inheritance in Java

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance (through Interfaces)
  5. Hybrid Inheritance (a combination of different types)

1. Single Inheritance

In single inheritance, a class (subclass) inherits from a single superclass. This is the simplest form of inheritance in Java.

Example:

// Superclass
class Animal {
void eat() {
System.out.println(“This animal eats food”);
}
}

// Subclass
class Dog extends Animal {
void bark() {
System.out.println(“The dog barks”);
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Method of Dog class
}
}

Output:

This animal eats food
The dog barks

Here, the Dog class inherits from the Animal class and gains access to its method eat().

2. Multilevel Inheritance

In multilevel inheritance, a class acquires from another class, and after that another class acquires from it, forming a chain of inheritance.

Example:

// Superclass
class Animal {
void eat() {
System.out.println(“This animal eats food”);
}
}

// Subclass
class Mammal extends Animal {
void walk() {
System.out.println(“This mammal walks”);
}
}

// Sub-subclass
class Dog extends Mammal {
void bark() {
System.out.println(“The dog barks”);
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal class
dog.walk(); // Inherited from Mammal class
dog.bark(); // Method of Dog class
}
}

Output:

This animal eats food
This mammal walks
The dogbarks
In this example, Dog inherits from Mammal, and Mammal inherits from Animal. This demonstrates a chain of inheritance.

3. Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses inherit from a single superclass. This allows different subclasses to share common behaviors from the same superclass.

Example:

// Superclass
class Animal {
void eat() {
System.out.println(“This animal eats food”);
}
}

// Subclass 1
class Dog extends Animal {
void bark() {
System.out.println(“The dog barks”);
}
}

// Subclass 2
class Cat extends Animal {
void meow() {
System.out.println(“The cat meows”);
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal class
dog.bark(); // Method of Dog class

Cat cat = new Cat();
cat.eat(); // Inherited from Animal class
cat.meow(); // Method of Cat class
}
}

Output:

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

In this case, both the Dog and Cat classes inherit the eat() method from the Animal class, demonstrating hierarchical inheritance.

4. Multiple Inheritance (through Interfaces)

Java does not support multiple inheritance directly through classes (i.e., a class cannot inherit from more than one class). However, multiple inheritance can be achieved using interfaces. A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources.

Example:

// Interface 1
interface Animal {
void eat();
}

// Interface 2
interface Flyable {
void fly();
}

// Class implementing both interfaces
class Bird implements Animal, Flyable {
@Override
public void eat() {
System.out.println(“The bird eats food”);
}

@Override
public void fly() {
System.out.println(“The bird flies”);
}
}

public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
bird.eat(); // From Animal interface
bird.fly(); // From Flyable interface
}
}

Output:

The bird eats food
The bird flies

Here, the Bird class implements two interfaces: Animal and Flyable. This allows Bird to inherit the behavior from both interfaces, which is an example of multiple inheritance.

5. Hybrid Inheritance

  • Hybrid inheritance refers to a combination of distinctive types of inheritance, such as a blend of multilevel inheritance and different inheritance utilizing interfaces.
  • Java does not specifically support hybrid inheritance utilizing classes, but it can be accomplished through a combination of class inheritance and interface execution.

Example:

// Interface 1
interface Animal {
void eat();
}

// Interface 2
interface Swimmable {
void swim();
}

// Superclass
class Mammal {
void breathe() {
System.out.println(“This mammal breathes air”);
}
}

// Subclass
class Dolphin extends Mammal implements Animal, Swimmable {
@Override
public void eat() {
System.out.println(“The dolphin eats fish”);
}

@Override
public void swim() {
System.out.println(“The dolphin swims”);
}
}

public class Main {
public static void main(String[] args) {
Dolphin dolphin = new Dolphin();
dolphin.eat(); // From Animal interface
dolphin.swim(); // From Swimmable interface
dolphin.breathe(); // From Mammal class
}
}

Output:

The dolphin eats fish
The dolphin swims
This mammal breathes air
Here, the Dolphin class combines both multilevel inheritance (from Mammal) and multiple inheritance (from Animal and Swimmable interfaces), forming a hybrid inheritance structure.

 

Leave a Comment