Inheritance is one of the four pillars of OOPS. Let’s have a look at it through a mind map.
Points to be noted about Inheritance in Java
- In inheritance, there is a parent-child relationship. i.e. a sub-class extends a super-class.
- “extends” and “implements” are the two keywords to be used to implement inheritance in Java.
- Every class in Java extends the Object superclass.
- A class cannot extend itself.
- To restrict the inheritance of methods and member variables, they need to be declared private in the super-class.
- Constructors and initializers both SIB(Static initialization block) & IIB(Instance initialization block) are not inherited by the subclass but they get executed during the instantiation of the subclass.
- When both super-class and sub-class have a field with the same name, the field in the super-class gets hidden but can be accessed using the super keyword.
- Static method members and variables are also inherited in the sub-class.
Order of execution of Constructor, SIB & IIB in Java Inheritance
The order of execution of inherited classes in Java is the following order:
- Static initializer block of each class
- Instance initializer block followed by constructor from parent to child
Check out our coding video for the same.
Feel free to share your thoughts on this topic in the comments section below 👇 We would be happy to hear and discuss the same 🙂