Inheritance is an important feature where a class can be extended and reused by another class.
A inherited class can be called as subclass or childclass.
The class which is inherited is called as Base class or Parent class or Super class.
The main advantage of inheritance is re-usability.
A class can be extended in java using keyword “extends“
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
class Card { int cardNumber; boolean isSwipeEnabled: ...... ...... } class CreditCard extends Card { int chipEnabled; int doubleAuthentication; ..... ..... } |
Here any object created for Card class have only 2 properties, however the Creditcard class object have 2 extra properies apart from 2 property extended from Card class.