INHERITANCE IN JAVA:
When one class called the derived class inherits the property of other class called the parent or base class is called Inheritance .
If class A is inherited from class B , it means that class A can access all the data members as well as methods of class B and class A can also define its own data members and methods . Inherited class can have its own properties also in addition to the properties of parent class .
IN Object Oriented programming , Inheritance is of five types . C++ supports all these five types . However in JAVA Multiple inheritance is not supported .
We use keyword 'extends' to inherit class .
Five types of Inheritance is :
We will explain these one by one .
SINGLE INHERITANCE
When a single derived class is inherited from single base class , such type of inheritance is called single inheritance . There is one base class and only one derived class .
When another derived class is created from a Derived class such type of inheritance is called multilevel inheritance .
HIERARCHICAL INHERITANCE
In Hierarchical Inheritance , from one single base class , more than one derived classes are inherited .
That is more than one class inherits the properties of one base class .
MULTIPLE INHERITANCE
In multiple inheritance class , one derived class is inherited from two base classes . A single class inherits the methods and data members of two classes .
JAVA , does not support multiple Inheritance . Now the question arises :
Why multiple inheritance is not supported in JAVA ?
In multiple programming properties of classes are inherited by same class . If both the base classes have a same function and we call this function in main , then which out of two function will be called ? Due to this ambiguity , multiple inheritance was removed by java Developers . In languages like C++ , this type of problem is solved by the concept of Virtual functions which is not supported in JAVA .
Note that even if both the bases classes do not have same function then also multiple inheritance is not possible in JAVA .
HYBRID INHERITANCE
Combination of above defined inheritances is called Hybrid inheritance . IN JAVA hybrid inheritance is possible but with a condition that there should be no multiple inheritance anywhere .
0 comments:
Post a Comment