Inheritance
Inheritance in Java allows you to create a hierarchy of classes where a subclass inherits the properties and behaviors of a superclass.
Parent Class: The class from which properties are inherited is called Parent Class. Parent Class is also called Super Class or Base Class.
Child Class: The class which inherits the properties of parent class is called Child Class. Child Class is also called Sub Class or Derived Class.
Here is a basic layout, how inheritance is implemented. We have to write "extends" keyword with base class and after that write the name of Parent Class.
Protected Access Modifier
The protected members are not accessible outside the class. They are only accessible only in child class.
Levels of Inheritance
- Single level Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Single level Inheritance
When one child class inherits attributes or properties or both from a parent class, this type of inheritance is called single level inheritance.
Multi-level Inheritance
In multi-level inheritance, we have multiple levels of inheritance. Multilevel inheritance occurs when a subclass is derived from another subclass.
Hierarchical Inheritance
In Hierarchical inheritance, we have multiple child classes and a single parent class.
In Java, we can not implement multiple and hybrid inheritance directly. We can achieve these concepts using interfaces.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP