Inheritance is a fundamental concept in object-oriented programming (OOP) and is an essential part of Java. It allows a class to inherit properties and behaviors from another class, creating a hierarchical relationship between classes. In Java, inheritance is achieved using the extends keyword. The class that is being inherited from is called the "parent class" or "superclass," and the class that inherits from the parent class is called the "child class" or "subclass."
Terms used in Inheritance
Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
Types of inheritance in java
Single Inheritance Example
When a class inherits another class, it is known as a single inheritance. In the example given below, Car class inherits the Vehicle class, so there is the single inheritance.
Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel inheritance.
Comments