In this I created a class named Stationary, which had a method (setter) named Setprice.
Then we inherited this class as base class in other class named as Penn (I didn't name it Pen as there already existed a class with that name in the package) and Pencil.
To inherit Stationary as base class in Penn and Pencil, child or derived class extends keyword was used.
To use the methods of base or parent class we perform function overriding which is a type of polymorphism.
@override is used as keyword to perform overriding, when a function with same name, same type of parameters as well as same number of parameters is used in subclass it is called as overriding.
Types of inheritance:
- Single level : One parent and one child class.
- Multi level: Child class of one acts as parent class for other.
- Hierarchical: One base class and more than one derived classes.
note: java does not support multiple inheritance of class, though multiple inheritance can be done in interfaces.
Top comments (0)