Inheritance in Java

Inheritance in Java
The process of obtaining the data members and methods from one class to another class is known as inheritance. It is one of the fundamental features of object-oriented programming.

Important points:

  1. In the inheritance the class which is give data members and methods is known as base or super or parent class.
  2. The class which is taking the data members and methods is known as sub or derived or child class.
  3. The data members and methods of a class are known as features.
  4. The concept of inheritance is also known as re-usability or extendable classes or sub classing or derivation.
  5. Why use Inheritance ?
  6. For Method Overriding (used for Runtime Polymorphism).
  7. It's main uses are to enable polymorphism and to be able to reuse code for different classes by putting it in a common super class
  8. For code Re-usability

Why use inheritance in java

  1. For Method Overriding (so runtime polymorphism can be achieved)
  2. For Code Reusability. 

Syntax of Java Inheritance

  1. class Subclass-name extends Superclass-name  
       //methods and fields  

    The extends keyword indicates that you are making a new class that derives from an existing class.
    In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.

Real life example of inheritance:

The real life example of inheritance is child and parents, all the properties of father are inherited by his son.

Advantage of inheritance

  1. If we develop any application using concept of Inheritance than that application have following advantages,
  2. Application development time is less.
  3. Application take less memory.
  4. Application execution time is less.
  5. Application performance is enhance (improved).
  6. Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost.

Tyes of Inheritance
Based on number of ways inheriting the feature of base class into derived class we have five types of inheritance; they are:
  1. Single inheritance
  2. Multiple inheritance
  3. Hierarchical inheritance
  4. Multilevel inheritance
  5. Hybrid inheritance

Single Inheritance

Single inheritance is damn easy to understand. When a class extends another one class only then we  call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be  a child class of A.

Single Inheritance example program in Java

Class A
   public void methodA()
     System.out.println("Base class method");

Class B extends A
   public void methodB()
     System.out.println("Child class method");
   public static void main(String args[])
     B obj = new B();
     obj.methodA(); //calling super class method
     obj.methodB(); //calling local method

 Multiple Inheritance

“Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.
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++.

Multilevel Inheritance

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. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A

Multilevel Inheritance example program in Java

Class X
   public void methodX()
     System.out.println("Class X method");
Class Y extends X
public void methodY()
System.out.println("class Y method");
Class Z extends Y
   public void methodZ()
     System.out.println("class Z method");
   public static void main(String args[])
     Z obj = new Z();
     obj.methodX(); //calling grand parent class method
     obj.methodY(); //calling parent class method
     obj.methodZ(); //calling local method

Hierarchical Inheritance

In such kind of inheritance one class is inherited by many sub classes. In below example class B,C and D inherits the same class A. A isparent class (or base class) of B,C & D
Hybrid Inheritance

In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple inheritance. A typical flow diagram would look like below. A hybrid inheritance can be achieved in the java in a same way as multiple inheritance can be!! Using interfaces. yes you heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java.

Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.

1.       class A{  
2.       void msg(){System.out.println("Hello");}  
3.       }  
4.       class B{  
5.       void msg(){System.out.println("Welcome");}  
6.       }  
7.       class C extends A,B{//suppose if it were  
9.        Public Static void main(String args[]){  
10.       C obj=new C();  
11.       obj.msg();//Now which msg() method would be invoked?  
12.    }  
13.    }  


Compile Time Error

How to achieve multiple inheritance in Java using interfaces?

interface X
   public void myMethod();
interface Y
   public void myMethod();
class Demo implements X, Y
   public void myMethod()
       System.out.println(" Multiple inheritance example using interfaces");

As you can see that the class implemented two interfaces. A class can implement any number of interfaces. In this case there is no ambiguity even though both the interfaces are having same method. Why? Because methods in an interface are always abstract by default, which doesn’t let them to give their implementation (or method definition ) in interface itself.

Search This Blog

Video of the Day