---
title: "Inheritance in Java"  
description: "In this article I am explaining about Inheritance in Java."  
author: "Manoj Pandey"  
published: 2015-04-20  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1732/inheritance-in-java  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Inheritance in Java

Java Inheritance defines an is-a [relationship](https://www.mindstick.com/articles/54917/how-to-keep-her-happy-in-a-long-distance-relationship) between a superclass and its subclasses. Inheritance represents the IS-A relationship, also known as parent-child relationship. Inheritance in java is a mechanism in which one object acquires all the properties and behaviours of parent object.\

The inheritance relationship is transitive: if class x extends class y, then a class z, which extends class x, will also inherit from class y.

For example a car class can inherit some properties from a General vehicle class. Here we find that the [base class](https://www.mindstick.com/blog/116/base-class-initilization) is the vehicle class and the subclass is the more specific car class.

##### What is not possible using java class Inheritance?

· Private members of the superclass are not inherited by the subclass and can only be indirectly accessed.

· Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.

· A subclass can extend only one superclass

· Members that have default [accessibility](https://www.mindstick.com/interview/209/describe-the-accessibility-modifier-protected-internal) in the superclass are also not inherited by subclasses in other packages, as these members are only accessible by their simple names in subclasses within the same package as the superclass.

Example of Inheritance

```
class Sample1{}class Sample2 extends Sample1{}
```

##### Types of Inheritance

1. Single Inheritance.

2. [Multiple Inheritance](https://www.mindstick.com/forum/159471/how-does-c-plus-plus-support-multiple-inheritance-and-what-are-potential-issues-associated-with-it).

3. [Multilevel Inheritance](https://www.mindstick.com/interview/23096/can-you-explain-me-multilevel-inheritance).

4. Hierarchical Inheritance

5. Hybrid Inheritance

\
1. Single Inheritance-: When a single [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) is created from a single base class then the inheritance is [called as](https://www.mindstick.com/forum/471/why-is-white-box-testing-called-as-glass-box-testing) single inheritance.

2. Multiple Inheritance-: In multiple inheritance, one class extends [multiple classes](https://www.mindstick.com/forum/159384/how-can-i-select-an-element-with-multiple-classes-in-jquery). Java does not support multiple inheritance but C++ supports. The above program can be modified to illustrate multiple inheritance. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.

Note 1: Multiple Inheritance is very rarely used in [software projects](https://answers.mindstick.com/qa/111803/what-are-the-common-pitfalls-to-avoid-when-designing-and-developing-software-projects). Using multiple inheritance often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class.

Note 2: Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple Inheritance is supported in C++.

3. Multilevel inheritance-: refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.

4. Hierarchical Inheritance-: When more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.

5. Hybrid Inheritance-: Any combination of single, hierarchical and multi-level inheritances is called as hybrid inheritance.

![Inheritance in Java](https://www.mindstick.com/mindstickarticle/2d57f8ff-6a32-416b-8c2f-e5ec44b5a335/images/d39f1373-1eb2-48cc-bc54-dc01aa9d0310.png)

![Inheritance in Java](https://www.mindstick.com/mindstickarticle/2d57f8ff-6a32-416b-8c2f-e5ec44b5a335/images/44b1311f-d956-41c9-8d3a-f900a505402e.png)

Here I am creating a sample of inheritance in java

```
class BaseClass {      // Method to find sum of give 2 numbers     public int FindSum(int x, int y) {           return (x + y);     }      // method to print given 2 numbers//When declared protected , can be accessed only from inside the derived     // class     // cannot access with the instance of derived class     protected void Print(int x, int y) {            System.out.println("First Number: " + x);           System.out.println("Second Number: " + y);      }} class Derivedclass extends BaseClass {      public void Print3numbers(int x, int y, int z) {           Print(x, y); // We can directly call baseclass members           System.out.println("Third Number: " + z);     } } class Sample {      public static void main(String args[]) {      // Create instance for derived class, so that base class members           // can also be accessed     // This is possible because derivedclass is inheriting base class      Derivedclass instance = new Derivedclass();  instance.Print3numbers(30, 40, 50); // Derived class internally calls base class method.           int sum = instance.FindSum(30, 40); // calling base class method with derived class instance           System.out.println("Sum : " + sum);      } }
```

Output-:

First Number: 30

Second Number: 40

Third Number: 50

Sum : 70

---

Original Source: https://www.mindstick.com/articles/1732/inheritance-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
