0

Difference between inheritance and polymorphism?

17th Feb 2017, 6:36 AM
Neha Selal
2 Antworten
+ 2
As far as I know, Inheritance allows you to use Base class's features. Polymorphism is a way to call the same method for different objects and generate different results based on the object type. For example: You have a base class "Animal" with "move()", "sleep()" and "eat()" methods. Derived classes "Cat" and "Rabbit" inherits "sleep" and "eat" methods, You can use it without redefining, these methods have already defined in base class. That's inheritance. But "move()" method should be different (running vs jumping). You override it in each derived class, so the same method "move()" generate different results for "Cat" and "Rabbit" class. That's polymorphism.
17th Feb 2017, 9:43 AM
Denis
0
thankyou for answering
17th Feb 2017, 5:02 PM
Neha Selal