Inheritance (IS-A Relationship)
It is a feature of object-oriented programming systems (OOPs). One class functionality is used for another class called inheritance. It is also known as the parent-child relationship—some following inheritance benefits.
- Inheritance is used for the reusability of code and runtime polymorphism.
- It is used for method overriding.
- A new class derived from an existing class is defined using the extends keyword.
A single class also uses inheritance
Syntax:- class <sub_class name> extends <super_class –name>
{
-----------
}
FAQs
Inheritance in Java is an Object-Oriented Programming (OOP) concept where one class acquires the properties and methods of another class. The class that inherits is called the child class (subclass), and the class whose properties are inherited is called the parent class (superclass).
Inheritance helps in code reusability, reducing redundancy, and improving program structure.
Single Inheritance – One child class inherits from one parent class.
Multilevel Inheritance – A class inherits from a class that is already inherited.
Hierarchical Inheritance – Multiple classes inherit from one parent class.
Multiple Inheritance (through Interfaces) – A class implements multiple interfaces.
The extends keyword is used in Java to inherit a class from another class.
Method overriding occurs when a child class provides a specific implementation of a method already defined in the parent class.
Inheritance is important because it:
Promotes code reusability
Reduces code duplication
Improves program organization
Supports hierarchical classification
Makes maintenance easier
No, Java does not support multiple inheritance with classes to avoid ambiguity problems (Diamond Problem). However, it can be achieved using interfaces.
Superclass (Parent Class): The class whose properties are inherited.
Subclass (Child Class): The class that inherits properties.
Advantages of inheritance include:
Code reuse
Easy program maintenance
Improved readability
Supports polymorphism
Reduces development time
RANREV INFOTECH