# Inheritance

Inheritance is a fundamental concept in Java's object-oriented programming (OOP) paradigm. It allows you to create a new class based on an existing class, inheriting the attributes and methods of the parent class. The class that is inherited from is called the "superclass" or "base class," and the class that inherits from it is called the "subclass" or "derived class."

Inheritance in Java can take several forms, each serving specific purposes. The common types of inheritance are:

<figure><img src="https://cdn-images-1.medium.com/max/1080/1*gvHEf4lT2m_dHyH6c0UC1Q.png" alt=""><figcaption><p>Types of Inheritance</p></figcaption></figure>

### 1. Single Inheritance

A class can inherit from only one other class. \
For example, the `Car` class can inherit from the `Vehicle` class.

{% code fullWidth="false" %}

```java
class Vehicle {
  String brand;
  String model;
  int year;
}

class Car extends Vehicle {
  int numberOfDoors;
} 
```

{% endcode %}

### 2. Multilevel Inheritance

In multilevel inheritance, a class can inherit from another class that is itself inherited from another class.

For example, the `SportsCar` class can inherit from the `Car` class, which inherits from the `Vehicle` class.

```java
class Vehicle {
  String brand;
  String model;
  int year;
}

class Car extends Vehicle {
  int numberOfDoors;
}

class SportsCar extends Car {
  boolean isConvertible;
}
```

### 3. H**ierarchical inheritance**

In hierarchical inheritance, there is a single superclass from which multiple subclasses inherit. \
For example, the `Animal` class can be inherited by the `Dog`, `Cat`, `Bird`, and `Fish` classes.

```java
class Animal {
  String name;
  int age;
}

class Dog extends Animal {
  String breed;
}

class Cat extends Animal {
  String furColor;
}

class Bird extends Animal {
  String wingSpan;
}

class Fish extends Animal {
  String waterType;
}
```

### 4. Multiple Inheritance

Java does not support Multiple Inheritance through classes. But with the introduction of default methods in Java 8, interfaces can have method implementations. it allows a class to implement multiple interfaces, achieving a form of multiple inheritance through interfaces.

For example, class Bird is inheriting classes Flyer and Swimmer

```java
interface Swimmer {
    void swim();
}

interface Flyer {
    void fly();
}

class Bird implements Swimmer, Flyer {
    // Implement swim() and fly() methods
}
```

### 5. H**ybrid Inheritance (NOT SUPPORTED IN JAVA)**

Hybrid inheritance combines multiple forms of inheritance in a single program.

Java does not support hybrid inheritance involving both class and interface inheritance due to the "**diamond problem**." It can lead to ambiguity in method resolution.

```java
class A {
    // ...
}

class B extends A {
    // ...
}

interface C {
    // ...
}

class D extends B implements C {
    // ...
}
```

### Diamond Problem

The diamond problem in Java is a situation that arises when a class inherits from two classes that have a common superclass. This can lead to ambiguity, as the subclass inherits two copies of the same method from its parent classes.

```java
class A {
  void doSomething() {
    System.out.println("A.doSomething()");
  }
}

class B extends A {
  void doSomething() {
    System.out.println("B.doSomething()");
  }
}

class C extends A {
  void doSomething() {
    System.out.println("C.doSomething()");
  }
}

// not possible with java classes
class D extends B, C {
}
```

The `D` class inherits the `doSomething()` method from both the `B` class and the `C` class. This creates an ambiguity, as the compiler does not know which version of the `doSomething()` method to call.

The use of interfaces is the preferred way to resolve the diamond problem in Java. This is because it allows us to avoid the ambiguity of multiple inheritance, and it also makes our code more modular and reusable.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codewithmeiy.gitbook.io/core-java/object-oriented-programming-oops/inheritance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
