Inheritance in Java:
In OOP, we often organize classes in hierarchy to avoid duplication and reduce redundancy. The classes in the lower hierarchy inherit all the variables (attributes) and methods (dynamic behaviors) from the higher hierarchies.A class in the lower hierarchy is called a subclass (or derived, child, extended class). A class in the upper hierarchy is called a superclass (or base, parent class).
By pulling out all the common variables and methods into the superclasses, and leave the specialized variables and methods in the subclasses, redundancy can be greatly reduced or eliminated as these common variables and methods do not need to be repeated in all the subclasses. Re usability is maximum.
A subclass inherits all the member variables and methods from its superclasses (the immediate parent and all its ancestors). It can use the inherited methods and variables as they are. It may also override an inherited method by providing its own version, or hide an inherited variable by defining a variable of the same name.
eg :
Person -- firstName,lastName
Student --firstName,lastName,grad year
Faculty -- firstName,lastName,yrs of experience , sme
OR
Emp-Mgr-SalesMgr scenario.
A subclass inherits all the variables and methods from its superclasses, including its immediate parent as well as all the ancestors.
It is important to note that a subclass is not a "subset" of a superclass. In contrast, subclass is a "superset" of a superclass. It is because a subclass inherits all the variables and methods of the superclass; in addition, it extends the superclass by providing more variables and methods.
Inheritance --- generalization ----> specialization.
IS A Relationship.
Why -- code re usability.
super class ---base class
sub class --derived class
keyword --extends
sub class IS A super class and something added(additonal state + additional behaviour) and something modified(behaviour ---method overriding)
Types of inheritance
1. single inheritance ---
class A{...} class B extends A{...}
2. multi level inhertance
class A{...} class B extends A{...} class C extends B{...}
3. multiple inhertiance --- NOT supported
class A extends B,C{...} -- compiler err
Why --For simplicity.
(Diamond problem)
We have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method. BUT which overridden method will be used? Will it be from B or C? Here we have an ambiguity.
Constructor invocations in inheritance hierarchy -- single & multi level.
eg -- Based on class A -- super class & B its sub class.
Further extend it by class C as a sub-class of B.
super keyword usage
1. To access super class's visible members
2. To invoke immediate super class's matching constructor --- accessible only from sub class constructor.(super(...))
eg -- Person,Student,Faculty
Emp,Manager,SalesManager
Shape, Circle,Rectangle
LoanAccount,HomeLoanAccount,VehicleLoanAccount,
Student,GradStudent,PostGradStudent
eg :
Person -- firstName,lastName
Student --firstName,lastName,grad year
Faculty -- firstName,lastName,yrs of experience , sme
Add the functionality to show individual details.
Regarding this & super :
1. Only a constr can use this() or super()
2. Has to be 1st statementt in the constr
3. Any constructor can never have both ie. this() & super()
4. super & this (w/o brackets) are used to access (visible) members of super class or the same class.
Sign up here with your email
ConversionConversion EmoticonEmoticon