Sart this post with a direcrt question. What do you do When you need to extend the functionalities of an existing object?
There are two different ways
1) Using Inheritance
baisc syntax is:
Why to use inheritance? Because it allows code resuability and also provides a common interfacefor similar obejects which display is-a relationship. Like Car is-a Veichle and Truck is-a Veichle. so we can define Veichle as the base class and other classes like Truck and Car can inherit attributes from it.
Why would you want that? let me show with a realistic example.
create a Polygon class. a Polygon is just a closed figure which has 3 or more sides. simplest of them is a rectangle.
now any closed geometric figure with some defined sides is-a Polygon . for ex. Triangle, here we can clearly see an IS-A relationship
so we sublclass Polygon in Triangle class
In Triangle class we inherited some attributes common to all Polygons like setting side lenghts and printing all sides,so When we want to set sides or print sides of any Triangle Object we did not have to define those methods again in Triangle class.