![]() |
VOOZH | about |
Inheritance in Java is a core OOP concept that allows a class to acquire properties and behaviors from another class. It helps in creating a new class from an existing class, promoting code reusability and better organization.
Dog barks Cat meows Cow moos
Explanation:
Syntax:
class Parent {
// fields and methods
}
class Child extends Parent {
// additional fields and methods
}
Note: In Java, inheritance is implemented using the extends keyword.
It defines the different ways a class can inherit properties and behavior from one or more classes.
Below are the different types of inheritance which are supported by Java.
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes, it is also known as simple inheritance.
This is a Vehicle This Vehicle is Car
In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also acts as the base class for other classes.
This is a Vehicle 4 Wheeler Vehicles This 4 Wheeler Vehicle is a Car
In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class. For example, cars and buses both are vehicle
This is a Vehicle This Vehicle is Car This is a Vehicle This Vehicle is Bus
In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes.
Note: that Java does not support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through Interfaces.
This is an AmphibiousVehicle This is a WaterVehicle This is a LandVehicle
It is a mix of two or more of the above types of inheritance. In Java, we can achieve hybrid inheritance only through Interfaces if we want to involve multiple inheritance to implement Hybrid inheritance.
This is a Vehicle This is a Car This is a Vehicle This is a Bus Fare information
Explanation:
IS-A represents an inheritance relationship in Java, meaning this object is a type of that object.
Now, based on the above example, in Object-Oriented terms, the following are true:
true true true
In sub-classes we can inherit members as is, replace them, hide them or supplement them with new members: