Inheritance in Java
Inheritance is one of he cornerstones of object-oriented programming because it allows the creation of hierarchical classifications. using inheritance, you can create a general class that defines traits common to a set of related items.
This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a super-class.
The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a super class. It inherits all the instance variables and methods defined by the super-class and add its own, unique statements.
What is Inheritance?
The philosophy behind inheritance is to portray things as they exist in the real world. For instance, a child inherits properties from both the parents. Inheritance means that a class derives a set of attributes and related behaviors from another class.
Inheritance helps you to:
- Reduce redundancy in code. Code redundancy means writing the same code in different places, leading to unnecessary replication of code. Inheritance helps you to reuse code.
- Maintain code easily, as the code resides at one place. Any changes made to the super-class automatically changes the behavior automatically.
- Extend the functionality of an existing class by adding more methods to the subclass.