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.
- In the inheritance the class which is give data members and methods is known as base or super or parent class.
- The class which is taking the data members and methods is known as sub or derived or child class.
- The data members and methods of a class are known as features.
- The concept of inheritance is also known as re-usability or extendable classes or sub classing or derivation.
- Why use Inheritance ?
- For Method Overriding (used for Runtime Polymorphism).
- 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
- For code Re-usability
Why use inheritance in java
- For Method Overriding (so runtime polymorphism can be achieved)
- For Code Reusability.
Syntax of Java Inheritance
classextends//methods and fieldsThe 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
- If we develop any application using concept of Inheritance than that application have following advantages,
- Application development time is less.
- Application take less memory.
- Application execution time is less.
- Application performance is enhance (improved).
- 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:
- Single inheritance
- Multiple inheritance
- Hierarchical inheritance
- Multilevel inheritance
- Hybrid 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
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” 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 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
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
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
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.
2. void "Hello"
5. void "Welcome"
7. class extends //suppose if it were
11. //Now which msg() method would be invoked?
Compile Time Error
How to achieve multiple inheritance in Java 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.