Member-only story
Inheritance in Java
Introduction
Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). In Java, inheritance allows one class to acquire the properties and methods of another class. It promotes code reusability, reduces code duplication, and makes programs easier to maintain.
The class whose properties and methods are inherited is called the parent class (superclass), while the class that inherits them is called the child class (subclass). Java uses the extends keyword to implement inheritance between classes.
Types of Inheritance in Java
Java supports several types of inheritance:
- Single Inheritance: One child class inherits from one parent class.
- Multilevel Inheritance: A class inherits from a child class, forming a chain of inheritance.
- Hierarchical Inheritance: Multiple child classes inherit from the same parent class.
- Multiple Inheritance: A class inherits from more than one parent class. Java does not support multiple inheritance through classes, but it supports it through interfaces.
Example of Inheritance in Java
Consider the following example, where a Dog class inherits the properties and methods of an Animal class.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {…




