---
title: "What is the Inheritance?"  
description: "What is the Inheritance?"  
author: "Krishnapriya Rajeev"  
published: 2023-04-29  
updated: 2023-05-01  
canonical: https://www.mindstick.com/forum/158076/what-is-the-inheritance  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# What is the Inheritance?

### What is the Inheritance?

## Replies

### Reply by Aryan Kumar

Inheritance is a mechanism in Java that allows a class to inherit properties and methods from another class. The class that inherits properties and methods is called a subclass or derived class, and the class from which it inherits is called the superclass or base class. Inheritance enables code reuse and helps to create a hierarchical structure of classes.

The syntax for inheritance in Java is as follows:

```java
class Superclass {
  // superclass fields and methods
}
class Subclass extends Superclass {
  // subclass fields and methods
}
```

In this example, the **Subclass** is inheriting from the **Superclass**. The **Subclass** can access all public and protected fields and methods of the **Superclass**, as well as any public fields and methods inherited from the **Object** class.

Subclasses can override methods inherited from their superclass using the **@Override** annotation. They can also add their own fields and methods or define constructors. The **super** keyword can be used to call the superclass constructor or to call a method from the superclass that has been overridden in the subclass.

Here's an example of inheritance in Java:

```java
class Animal {
   protected String name;
   public Animal(String name) {
       this.name = name;
   }
   public void eat() {
       System.out.println(name + " is eating.");
   }
}
class Dog extends Animal {
   public Dog(String name) {
       super(name);
   }
   @Override
   public void eat() {
       System.out.println(name + " is eating dog food.");
   }
   public void bark() {
       System.out.println(name + " is barking.");
   }
}
```

In this example, **Dog** is a subclass of **Animal**. It inherits the **name** field and **eat()** method from **Animal**, but it overrides **eat()** to print a different message. It also adds a new method **bark()**.

\

### Reply by Krishnapriya Rajeev

Inheritance allows one class to derive properties and behavior from another class. The class that inherits properties is called a subclass or derived class, while the class that provides the properties is called the superclass or base class.

The subclass can inherit the following properties and behavior from its superclass:

- The subclass can inherit instance variables and methods from its superclass. It can use these variables and methods as if they were defined within the subclass itself.
- The subclass can inherit constructors from its superclass. It can use these constructors to create objects of the subclass.
- The subclass can inherit access modifiers from its superclass. It can access public and protected members of the superclass, but not private members.

To inherit properties and behavior from a superclass, the subclass must use the "extends" keyword in its class definition. For example, the following code defines a subclass "Employee" that inherits properties and behavior from its superclass "Person":

```plaintext
public class Person {
    protected String name;

    public Person(String name) {
        this.name = name;
    }

    public void printName() {
        System.out.println("Name: " + this.name);
    }
}

public class Employee extends Person {
    private int employeeId;

    public Employee(String name, int employeeId) {
        super(name);
        this.employeeId = employeeId;
    }

    public int getEmployeeId() {
        return this.employeeId;
    }
}
```

In this example, the subclass "Employee" extends the superclass "Person" using the "extends" keyword. The subclass inherits the "name" instance variable and "printName" method from the superclass, and it adds its own instance variable "employeeId" and method "getEmployeeId".

By using inheritance, we can reuse code and create hierarchies of related classes. It also helps in reducing code duplication and improving code maintainability.


---

Original Source: https://www.mindstick.com/forum/158076/what-is-the-inheritance

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
