Java Inheritance defines an is-a 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 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 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.

3.      Multilevel Inheritance.

4.      Hierarchical Inheritance

5.      Hybrid Inheritance 

1.    Single Inheritance-: When a single derived class is created from a single base class then the inheritance is called as single inheritance. 

2.    Multiple Inheritance-: In multiple inheritance, one class extends multiple classes. 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. 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.






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);







First Number: 30

Second Number: 40

Third Number: 50

Sum : 70

  Modified On Dec-05-2017 02:14:51 AM

Leave Comment