# Inheritance

## 👪 What is Inheritance?

**Inheritance** in Java allows a class to inherit attributes and behaviors from another class. This promotes code reuse and establishes hierarchical relationships between classes.

## Why use inheritance in java

* For [Method Overriding](/qa/java/oops/polymorphism.md) (so runtime polymorphism can be achieved).
* For Code Reusability.

## 💡 Rules for Inheritance in Java 👨‍💻

Inheritance in Java follows these main rules:

* 🟢 Uses "extends" keyword to inherit from superclass

```java
class Dog extends Animal {
  //...
}
```

* 🟡 Subclasses inherit public and protected members
* 🔴 Private members not inherited
* 🟣 Single inheritance - one superclass allowed
* 🟢 Subclass can override superclass methods

```java
@Override
int getAge() {
  // overridden method
}
```

* 🟡 Overridden methods must have same signature
* 🟢 Use super() to call superclass constructor
* 🟢 Subclass can declare new members
* 🟡 Subclass can reuse superclass code
* 🔵 Inheritance represents an "is-a" relationship
* 🟣 Use interfaces for "has-a" relationship
* 🔴 Avoid overusing inheritance where aggregation works better

Inheritance enables code reuse and establishes rich relationships between classes. But careful design is needed to maximize benefits.

## 👨‍👦 Parent and Child Classes

In Java, classes can inherit from other classes using the `extends` keyword:

```java
public class Vehicle {

  public void startEngine() {
    System.out.println("Engine started.");
  }

}

public class Car extends Vehicle {
  // Car inherits the startEngine method from Vehicle
} 
```

Here `Car` inherits from the `Vehicle` class.

## 🔀 Key Notes

* Inheritance allows a class to inherit from another parent class
* The child class gets all the members of the parent class
* This establishes a hierarchical parent-child relationship
* Inheritance supports code reuse in Java

So with inheritance, classes can reuse logic in other classes!

<figure><img src="https://images.unsplash.com/photo-1570018144715-43110363d70a?crop=entropy&#x26;cs=srgb&#x26;fm=jpg&#x26;ixid=M3wxOTcwMjR8MHwxfHNlYXJjaHw4fHxraXR0ZW58ZW58MHx8fHwxNjg5OTI4ODIzfDA&#x26;ixlib=rb-4.0.3&#x26;q=85" alt=""><figcaption></figcaption></figure>

## 👨‍👦 In Simple Terms

Inheritance allows classes to form hierarchical parent-child relationships, just like children inherit genes and traits from their parents.

The child class automatically gets the fields and methods of the parent class, allowing code reuse.

So in Java, inheritance represents parent-child class relationships!


---

# 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://qatesting.gitbook.io/qa/java/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.
