Question:
polymorphism help ....?
anonymous
2009-01-16 11:16:26 UTC
What’s the difference between Polymorphism and regular inheritance
Three answers:
joel_corley
2009-01-16 14:00:56 UTC
The word "polymorphic" means "relating to the occurrence of more than one kind" or "many morphisms" or "many abstract structures".



In computer programming, polymorphic is a characteristic of a class definition. This characteristic allows you to change how the class behaves in response to given stimulus.



Since a stimulus is generally viewed as a method or function, polymorphism is implemented through the use of what are commonly called virtual functions or methods. Virtual functions are ones who's definition can be changed or overridden - usually at compile time.



Regular inheritance is simply the implicit transference or transcription of the various attributes of one class into the definition of another class. In object oriented programming, functions or methods are attributes of the definition of a class and are generally inheritable by another class definition.



This is true even of virtual functions or methods; however with normal inheritance, a reference to a particular base type of object retains the associated function definitions of the base class and invocation of those functions will not normally call a re-definition of the function, even if the object actually referenced is actually of that derived-most type. However with virtual functions, the invocation of a function will correctly execute the definition associated with the object's actual type and not simply the definition defined by the base class.



To summarize, polymorphism allows you to change the behavior of a collection of objects based solely on each object's type at creation. It is not necessary for the caller to know the object's derived-most type in order to invoke the correct definition of that function. On the other hand, normal inheritance would simply have the caller invoke the same function for all callers and that function would be the one defined in the base class because the caller has no idea from which derived-most class each object is constructed.



As an example, consider the concept of a simple game program. Say the game contains two types of objects - circles and boxes. The game could define both the Circle and Box classes from a base class called Object. When the game needs to draw the objects in the game, it can invoke a function defined in Object called Draw() for each object it holds. Rather than Object::Draw() knowing how to draw Circle and Box objects, it defines them as virtual and the Circle and Box objects can define how to Draw() for themselves.



I hope that makes it more clear.



- Joel
?
2016-10-25 04:41:07 UTC
"Polymorphism" is function to "merchandise oriented" programming languages. it is that this function that permits an merchandise to regulate implementation, and yet preserve "files form". Inheritance is only a fashion to component code by increasing specialization. it isn't even arguably the acceptable technique, even though it does shop on with somewhat obviously from the theory that gadgets with a similar implementation can share code, that that code should be amassed, and a taxonomy should be outfitted. although, each technique in an merchandise should be autonomous, and the concept of "classification" (taxonomy) should be disbursed with. Languages like "self" are examples of diagnosis in those traces. different languages that reveal polymorphism yet at the prompt are not classification depending (no inheritance) are lua and ecmascript. yet... given the way the question is phrased, the answer is "all merchandise oriented programming languages". The exemplar being sought right here's smalltalk and c++. wish this permits.
anonymous
2009-01-16 11:29:51 UTC
These are 2 different concepts, which work together and are cornerstones of OOP.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...