Q1: OOPS: Inheritance?
Q2: When to use?
Q3: Advantages?
Q4: Disadvantages?
Q5: Explain types with example?
Q6: Facts about inheritance?
Q7: Why java does not support multiple inheritances?
Q8: How to access private members of the superclass?
1. Inheritance:-
1.1 it is a mechanism of acquiring certain properties and behaviour of one class and another class i.e. parent class to child class.
1.2 the purpose of inheritance is to be able to create new classes that are built upon existing classes such as new child class on the existing parent class.
1.3 inheritance represents an IS-A-relationship i.e. parent-child relationship.
1.4 access modifiers play a major role in inheritance. Only public and protected members can be accessed by child class and not the private members. Although we can indirectly use private members using public or protected functions.
Click here to view the code for 1.4 point
Syntax and terms used
class Subclass-name extends Superclass-name {
//methods and fields
}
-: class - it is a group of objects with common properties. it is a template or blueprint from which object can be created.
-: subclass - class that inherits another class. It is also called a derived class, extended class, or child class.
-: superclass - class from where a subclass inherits the features. It is also called as a base class or a parent class.
-: extends - indicates the new class is inheriting from the existing class.
Example Below source code
2. When to use:-
2.1 Method Overriding to achieve runtime polymorphism
2.2 Code reusability
3. Advantages:-
3.1 reusability
3.2 readability
3.3 reliability i.e. the base class code will be already tested and debugged
3.4 less costly because of reusability
3.5 supports code extensibility
3.6 improper use or less knowledge may lead to wrong solutions
4. Disadvantages:-
4.1 less use of data members present in the base class may lead to memory wastage.
4.2 dependability may cause unintended damage i.e. inheritance causes the coupling between classes, thus a change in the base class will affect all the child processes.
4.3 can't inherit from final class i.e. below program will an error.
final class Class1 {...
}class Class2 extends Class1{
...
}
-
Single Inheritance
1.1 subclass extends or inherits the features of one superclass. eg. daughter class is inheriting father class
1.2 Basically, java only uses a single inheritance as a subclass cannot extend more superclass.
CLICK HERE FOR SIMPLE PROGRAM
-
Multilevel Inheritance
2.1 a class will be inheriting a base class and as well as the derived class also act as the base class to other class. eg. daughter inherit form father, and father inherit from grandfather
CLICK HERE FOR SIMPLE PROGRAM
-
Hierarchical Inheritance
3.1 one class serves as a superclass for more than one subclass. eg. one teacher serves multiple students
CLICK HERE FOR SIMPLE PROGRAM
-
Multiple Inheritance (can't be implemented using class in java)
4.1 one class can have more than one superclass
4.2 java does not support multiple inheritances with classes, but it can only be achieved through interfaces
CLICK HERE FOR SIMPLE PROGRAM
-
Hybrid Inheritance
5.1 combinations of two or more types of inheritance.
5.2 since multiple inheritances can't be achieved in java with classes, hybrid inheritance also can't be achieved in java with classes. But using inheritance only it can be implemented.
CLICK HERE FOR SIMPLE PROGRAM
6. Facts about inheritance:-
6.1 only one superclass: The superclass can only be one, that is because java does not support multiple inheritance with classes.
6.2 inherit constructor: derived class can inherit methods, variables and nested classes from its superclass but constructors, that is because constructors are not members.However, constructors of the superclass can be invoked from its derived class
6.3 inheritance does not allow derived class to inherit private members of its superclass.
However, it can be accessed using getters and setters in order to access them.
7. Why java does not support multiple inheritances:-
7.1 To prevent ambiguity: consider a situation where both BaseClass1 and BaseClass2 has calculate() with different working or behaviour, then there will be ambiguity if ChildClass which inherits both the base classes will try to call calculate()
because java compiler can't decide which calculate() it should inherit.
8. How to access private members of the superclass?
We can use getter and setter methods that
Click here for the codeoption 1. return private member
option 2. return public member that store private member's values
Please comment if you have any feedback or suggestions
Top comments (4)
Nice read! I have a small suggestion. When you have a piece of code in your article, rather than pasting pics it's better to use markdown for the code :)
Thank you for your suggestions sir. Feedbacks and suggestions are always welcome.
Good and detailed explanation...π
Thank you so much, Sir. :)