Friday 27 February 2015

Inheritance in Java

         Inheritance is the process by which one object acquires the property of another object.Using inheritance you can create a general class which has all related items and this class can be inherited by other classes. The inherited class in called superclass and the class that does inheriting is called subclass in Java.

You can inherit a class using the keyword extends. Below is the simple example for inheritance.
You can copy code by select+ctrl+C
 


Output:

 





Types of Inheritance:
1) Single Inheritance
2) Multiple Inheritance
3) Multilevel Inheritance
4) Hierarchical Inheritance
5) Hybrid Inheritance

1)Single Inheritance
If a class inherited to only one class then we call that inheritance as single inheritance.



Here Class A Extends to Class B here the inheritance is only one level .

2)Multiple Inheritance
If a class Inherits property from more than one class is called Multiple Inheritance.

Java does not supports Multiple inheritance by using Extends. We can achieve this by Implementing the interfaces. We will discuss more about this in Interfaces in Java section.

3)Multilevel inheritance.
One class inherited to another class and this class will be base class for next class.

Here Class A Inherited to Class B and class B is inherited to Class C . Class B will become base class for class C. Class C will have the properties of both the class A and B.

4) Hierarchical Inheritance
If the properties of one class is inherited to multiple classes then this inheritance is called the hierarchical inheritance.

5)Hybrid Inheritance
Hybrid inheritance is the combination of Hierarchical and multiple Inheritance. We can achieve Hierarchical inheritance using Extends and we can achieve Multiple inheritance using Implements .


No comments: