0% found this document useful (0 votes)
46 views

Advantages of Inheritance

Inheritance allows child classes to inherit members from parent classes, avoiding redefinition. It establishes reusability, with child classes gaining parent methods and fields. The most common methods are defined in parent classes to be inherited. Inheritance reduces code and saves time by promoting reusability. Object is the root class for all Java classes.

Uploaded by

Zishan Ahamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Advantages of Inheritance

Inheritance allows child classes to inherit members from parent classes, avoiding redefinition. It establishes reusability, with child classes gaining parent methods and fields. The most common methods are defined in parent classes to be inherited. Inheritance reduces code and saves time by promoting reusability. Object is the root class for all Java classes.

Uploaded by

Zishan Ahamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Advantages of Inheritance

Whatever member parents have, automatically these are available to child class, so not required to redefine.

Above program, Class C have two methods named m1 () and m2 (). So reusability established.
 Parent object cannot call child class method.
 Parent reference can be used to hold of child class object (Polymorphism concept), but by using this
reference we cannot access child class specific method.
 And child class cannot hold parents object.

Why Inheritance & Rules of?

www.facebook.com/liar.zishan
The most common methods of all child classes are writing on parent class, from this concept inheritance
comes. In child class only child specific methods are implemented.
Inheritance thus:
o Reduce code and save time
o Reusability of code
*** What is the super class or root of all java classes? Answer is object classes. As object class holds most
commonly records for all java classes. Throw-able is root of all exception error hierarchy which has common
methods for exception and error child classes.

www.facebook.com/liar.zishan
1. Single Inheritance: Single child extends one parents class.
2. Multiple Inheritances: More than one parent class extended but not supported in java, for this reason
interface applied. This is not supported for ambiguity and diamond access problem.

3. Multi-Level Inheritance: In multi level class B extends A then class C extends B, and then all
properties of A will be automatically available in C classes respectively.
Example: renowned actor fame also exist for actor child and then next child also, so child is the luckiest
person.

4. Hierarchical Inheritance: Multiple children available for parent class. Actor has several children. It is
the reverse of multiple inheritances.
5. Hybrid Inheritance: using multiple type or groups of inheritance simultaneously. But not supported in
java as multiple not supported in java.

www.facebook.com/liar.zishan
6. Cyclic Inheritance: [not supported] not needed really as if both A and B wants one another then make
this two classes as one class.

Why Multiple Inheritance not Support?


1. Java supported multiple interface but only in interfaces not classes and skip ambiguity/diamond
access problem.

www.facebook.com/liar.zishan
2. If our class not extends any class then only super object associated with it.

**** Internally even not support multiple inheritances though object is super class of all. So interviewer may
be confused you by below first picture but u should answer from figure 2 which one is correct.

www.facebook.com/liar.zishan
****Java not supported three inheritances: Multiple, cyclic and hierarchical.****
Class A A a = new C();
{ This valid as parent can hold child object but cannot
} call child method.
Class B extends A
{ C object

} A a--
Class C extends B
{
}

www.facebook.com/liar.zishan

You might also like