If I need to declare the same method every time I extend the class then what is the use if we declare method again? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

If I need to declare the same method every time I extend the class then what is the use if we declare method again?

May I use the same method with some changes in inherited class?

9th Aug 2020, 10:46 AM
Yash Patil
Yash Patil - avatar
2 Answers
+ 2
yes, just make a new method with the same name and whatever changes in the child class. When an instance of the child class calls the method, it will be the overridden version. Thats at least how it is in Python
9th Aug 2020, 10:52 AM
Slick
Slick - avatar
+ 1
Slick is correct theres no other way to tell the 2 methods apart unless you mention the overridden method in sub class, Once you have created an instance of the sub class but used parent class as the refrence type the correct method will be called on your sub class instance during runtime your object will always act like it should because it is of couse a sub class object. This works because of polymorphism as the parent Class should know the basics of what its sub classes can do.
9th Aug 2020, 8:58 PM
D_Stark
D_Stark - avatar