Talk:Polymorphism in object-oriented programming

Untitled
Shouldn't Smalltalk be mentioned? Wasn't it the first language to have polymorphism? —Preceding unsigned comment added by 216.27.163.78 (talk) 03:13, 27 April 2009 (UTC)

Older comments
I would say that being able to respond to method calls by their names is only a consequence of polymorphism, and is caused by the methods being virtual. Polymorphism is the ability of treating objects of classes also as objects of all their superclasses, in all aspects (thus being able to use their members such as methods). —Preceding unsigned comment added by Balrog-kun (talk • contribs) 29 March 2006


 * Actually, it's the other way around. What you're describing is called inheritance, and polymorphism is a consequence of inheritance. All systems using inheritance are polymorphic, but not all polymorphic systems use inheritance (in Smalltalk you can use inheritance, but it's not necessary), or even have the concept of "class" (like Self or Javascript which are based on prototypes instead of classes).--Cynebeald (talk) 17:44, 23 January 2008 (UTC)

polymorphism manupulates data.

Is it true that one can program the system to jump over clssses i.e., I want some data but not all from each class, cub class, partial class, and supr class however, I do not want to have to search each class thereby slowin g down my processing power or taking up more processor power therteby slowing down my time to transmit. I want to  data fro the Class and the supper class by settin g pointrs skipping the sub and partial clases using if then statem e In nts. If this x in class skip to super c;ss and data and. This is imperative for keeping real time systems.

Thoughts,

Mike

Mike_Benso@yahoo.com

—Preceding unsigned comment added by 69.136.233.19 (talk) 11 December 2006

short description
polymorphism as such has nothing to do with inheritance. Polymorphism means than objects that implement the same method name can be exchanged. The definition given seems to stem from static typing. —Preceding unsigned comment added by 89.62.63.186 (talk) 13 February 2007

#include
is the #include even necessary? Maybe I didn't read it well enough, but it seems an array is being used not a vector - CKL —Preceding unsigned comment added by 71.210.245.151 (talk) 3 July 2007

VB.NET Example
Why is this using interfaces rather than inheritance which all the other OO examples use? This make the examples inconsisteny in what they are actually doing. Surely it would be better for consistency to show the inherited example with the interface one as an extenison of this. —Preceding unsigned comment added by 81.133.167.102 (talk) 16:42, 14 December 2007 (UTC)
 * I agree, inheritance would be the more natural way to do this. 83.166.176.136 (talk) 14:07, 9 July 2008 (UTC)
 * In addition, the article implies below the VB example that it's a deficiency in Java that leads to requiring an abstract class rather than the use of an abstract class being the correct semantics. Could someone with the required knowledge please edit the VB example so we can get rid of this misleading comment while maintaining sense? Morticed (talk) 11:21, 1 August 2008 (UTC)

Java Example
The Java example is dealing with Shapes, while all the other examples are dealing with Animals. —Preceding unsigned comment added by 199.26.230.102 (talk) 16:04, 20 December 2007 (UTC)
 * I converted the C++ version to Java and added it to the article in place of the Shapes example. I forgot to log in, though, so it shows up as my IP. --Powerlord (talk) 06:19, 26 February 2008 (UTC)

Java Section
This is just the concept of Inheritance and does not show polymorphism? Please email your response to siu02rk@hotmail.com

Thanks

90.211.234.107 (talk) 10:08, 19 May 2008 (UTC)

Delphi Section
I have no idea what DEEP is, in relation to either Delphi or Object Pascal. Neither does Wikipedia nor Google. I'm removing it. I'm adding in it's place, a reference to FPC and Object Pascal and linking all three to their respective articles. Perhaps somebody with time on their hands can do likewise with the other languages. --Ian Macintosh (talk) 19:49 25 July 2011 (UTC)

syntax of the examples
Not strictly syntax, but some examples of the dog said "Arf! Arf!" and some said "Woof! Woof!". I'm changing them all to "Woof! Woof!" for consistency. Further, there is some other inconsistency with examples, some using Cat, Dog and others using Missy, Mr. Mistoffelees and Lassie. I'm thinking that Cat, Dog are more acceptable and require less knowledge of some specific English television programs and operas. Again, somebody with some time on hand might want to polish the other code examples, where appropriate, into the Cat, Dog example format --Ian Macintosh (talk) 19:49 25 July 2011 (UTC)

JAVA language example:

protected Animal(String name) {   this.name = name; }

this particular line of code does not give correctness, this method needs a return type. so what is it?

Xmlv (talk) 21:08, 20 November 2008 (UTC)

C# language example:

Using VS 2008 Express the example does not compile giving an error: Error	1	'AnimalBase.AnimalBase(string)' is inaccessible due to its protection level Program.cs	40	9	Animal

I am thinking AnimalBase is getting protected from the line: private string _name; to fix I added the word public in front of AnimalBase: public AnimalBase(string name)

Note on 4/11/09 from original author: I think protected is best for the constructor, Name has to be public to satisfy the interface, but the class should be marked abstract. I made those changes. The rationale is that the design of the Animal Base class is to function as purely a base class. It cannot be instantiated, only inherited from. —Preceding unsigned comment added by Tiburondude (talk • contribs) 21:18, 11 April 2009 (UTC)

Overloading is not Polymorphism
I understand that polymorphism is completely different from overloading, therefor, I think the comments relating both concepts should be removed from the article. Ramiro Pereira de Magalhães (talk) 14:27, 11 January 2009 (UTC)


 * Actually... This is at odds with the content of the article on Type_polymorphism, which claims that overloading is a form of ad hoc polymorphism. My compilers and programming languages classes (both grad level) agree with the type polymorphism article, so I think we need to distinguish between these different uses of the term "Polymorphism". The cited reference in this article for claiming that they are not the same is Bjarne Stroustrup, and within the context of C++ such a comment makes total sense because they are completely different language features implemented in completely different ways. But it would appear that, as a property of languages in a more formal sense, polymorphism is broader than just subtype substitutability. I'd like to see that paragraph altered to recognize the distinction. Workaphobia (talk) 17:57, 15 April 2009 (UTC)

C# code commentary
Under the C# code it says "This is a slightly modified implementation of the Strategy pattern". Well I don't think so. I'd say it's the most regular use of inheritance. Just having the subclass instances in a List doesn't make it "Strategy".

Reply
Sorry if this is written unclearly. Appreciate the editing help. The intention is to say that since the strategy pattern usually has read/write properties of type interface, and the example has a read only property of type interface with the only way to set the "strategy" interface is via the constructor. That's what I mean by slightly modified implementation of the strategy pattern. Having a generic list of interfaces is completely unrelated to the strategy pattern. Thanks! —Preceding unsigned comment added by 76.126.198.46 (talk) 20:55, 11 April 2009 (UTC)

C# code should be simplified
The C# code mentions the use of interfaces then implements polymorphism in two ways. It use an abstract base class (AnimalBase) and an interface (IAnimal). I suggest the example just use an interface for simplicity (or at least the commentary should explain what is happening). A second example using an abstract base class could also be included or, alternatively, a note that it can be done in almost identical fashion to the C++ abtract (pure virtual) base class.

Also the use of "var" might be best (and is easily) avoided. Not everyone is using the latest version of C# and it does not hurt the example to not use it. AndrewWPhillips (talk) 20:29, 25 June 2009 (UTC)

Removal of Mitigating Circular Dependency References Using Polymorphism In C# section
Hi, just writing a short note why I removed this section (I forgot to log in, but it was me :)), I think the section would probably make an interesting blog post, but it is not suitable for this article. It does not describe polymorphism, it describes something that uses it to achieve a very specific unrelated goal. Nor does it help the reader to better understand polymorphism. If you would insist on keeping it on Wikipedia, it would be less out of place in an article about circular dependency references. Additionally, it provided very little introduction, dropping the reader right into a technical detail of a very a different subject. Grauw (talk) 00:19, 13 January 2010 (UTC)

Polymorphism is not exactly what this article claims it is
OK, "totally wrong" as I said in the edit change log is too strong. However, it's common to use "polymorphism" to refer to what's called function overloading here in Wikipedia, i.e. the ability to have different versions of the same-named function that take different numbers and/or types of arguments. The article here specifically claims that polymorphism is not function overloading, which is simply wrong. Take a look at many sources, e.g. www.programmersheaven.com/2/FAQ-JAVA-What-Is-Polymorphism (which I can't directly insert a link to) and, which refer to function overloading as one type of polymorphism. The second refers to the sort of polymorphism as discussed in this article as "dynamic binding polymorphism". See also this classic paper which describes the theoretical background of polymorphism:. This paper uses the term "parametric polymorphism" to refer to polymorphism of the function-overloading type. Benwing (talk) 23:55, 12 October 2010 (UTC)

Remember that this is an article specifically about Subtype Polymorphism, and it's true to say that Subtype Polymorphism is not the same as function overloading/overriding on their own (i.e. there needs to be derivation, or an interface). That said, I do feel this article is badly structured and misleading. The references you have provided explain the topic much more clearly. (talk) 08:59, 23 October 2010 (UTC)

Trying to understand the first sentence of the article.
After further thinking, I removed my Perl (counter) example and comments, because I have even more basic questions, and, in retrospect, my example was not quite right.

Here is the first sentence of the article:

''Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form. ''

I think the sentence is unclear about who creates the variable, function or object. After some thinking I believe the sentence want to say that it is the programmer whose ability we are talking about. So maybe, the sentence should be changed to (for clarity):

''Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability of the programmer to create a variable, a function, or an object that has more than one form. ''

Still, I do not really like this sentence. What does it mean to create a variable that has more than one form? What is the form of a variable? Same problem with a function. Does creating a function means writing a function? How can one function have multiple forms? A function identifier might refer to multiple functions in different contexts. I understand this. For an object, does creating it means instantiating it? Or is it the act of writing the code for the class that the object belongs to?

Now, if we mean the act of instantiation: What does it mean that the object created can have multiple forms? What does it mean for the object to have a single form? What is a form? Type? I could go on.. But it would be nice if someone would clarify these things before I go any further (I feel that I am already speculating too much about the intention of the author). In summary, a little more precision would help. Szepi (talk) 04:49, 5 February 2011 (UTC)

Bad article... Bad article!
This is a classic example of a lousy encyclopedia article. It's written in a stiff academic style that even Lilith Sternin would disapprove of. Worse, the author has no idea of how to organize and present the material to make it accessible and interesting. "Accessible and interesting" does not equate to "simplistic" or "inaccurate". One of the marks of a good writer is the ability to present complex material without watering it down (qv, Richard Feynman).

In short... it's BOR...ing. I quit after the first two paragraphs. It needs not only a complete rewrite, but a complete re-conceptualization. WilliamSommerwerck (talk) 19:40, 2 September 2011 (UTC)

Cut down examples
Restored the examples, but then removed all but two. The ones I've chosen are C++ and Java, purely because this will cover a wide range of users, JavaScript is not OO and therefore is not a suitable example for this article, and Java/C/C# users should all be able to interpret the Java example quite easily. Carl Sixsmith (talk) 07:58, 26 October 2011 (UTC)


 * I actually chose javascript because it wasn't OO, that way we had one example of polymorphism in an object context, and one in a function context. Why do we need two OO examples? The other benefit of the javascript example is that it's very simple. This is also true of the PHP and Python examples, which show the concept succinctly without getting bogged down in language-specific features and keywords that may not be accessible to a global audience. For instance, the java example (which is one of the best of the bunch), necessarily uses generics, annotations, and package imports inline between classes, without making it clear that they each need to be broken into separate files (why is that distinction important here anyway). I understand those concepts because I program in Java, but others may not. The C++ example has even more jargon. While that problem isn't "the end of the world", so to speak, it isn't a problem shared by other language examples we have readily available. What we need is a clear example in psuedocode (or as close to it as we can get), which is accessible to everyone from every background, with as few features as possible, which only serves to clearly highlight polymorphism, and nothing else. I don't believe the Java and C++ examples do that as well as others we could have chosen.  &mdash; Jess &middot; &Delta;&hearts; 16:43, 26 October 2011 (UTC)


 * The article is titled Polymorphism in object-oriented programming not polymorphism in general, that was my thinking behind leaving two OO examples. I'm not precious on the choice of languages though and agree the examples should be as simple as possible. According to http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html Java is still the number 1 popular programming language, that was the reasoning behind leaving that one in. Personally I'm a .net developer so would prefer a simple example in C#, but I went for the populist approach. I'm not too concerned with which gets chosen though. Carl Sixsmith (talk) 18:42, 26 October 2011 (UTC)


 * You're correct about the scope of the article being OO. Throughout the article, we refer to "polymorphism" generally, so the additional specificity in the article title sort of slipped my mind. That would, indeed, make the javascript example not relevant. After simplifying the java example a little bit, and changing C++ to Python, I'm actually okay with it. I still think the java example is really long, but PHP is too close to Python to make a second example of it useful, and the others I'm not so sure about. I think the current state of things is okay, assuming you're also fine with it. I'll assume you are based on your above response, but let me know if you have any other concerns.  &mdash; Jess &middot; &Delta;&hearts; 20:45, 26 October 2011 (UTC)
 * Looks OK to me, I think within programming readers Java is better as it has a higher following, none programmers would probably be better served with a simpler C# example, but how many non-programmers are going to be interested in this article? I'm going to see if I can't add some references/clean up the rest of the article now. Carl Sixsmith (talk) 06:38, 27 October 2011 (UTC)

Didn't Understand
This article doesn't tell what Polymorphism is. I read the article for four times. It fails to define Polymorphism. It fails to explain Polymorphism. It fails to distinguish it from function overloading.

Please -someone knowledgable- write a definition at the beginning and an explanation in the second paragraph. Then indicate the difference from overloading.--129.7.147.112 (talk) 16:03, 15 December 2011 (UTC)

Needs Merging
Since this article is so small and deals with a subdivion of the original concept, i feel it should be merged with the main article for polymorphism. In doing so it would shed light on the inaccuracies people have been complaining about and thus increase the chance of someone fixing the mistakes, there is no need for a separate article to discuss something that only needs a brief explanation to distinguish its use in OO vs other paradigms. — Preceding unsigned comment added by Konnerjr (talk • contribs) 16:58, 21 December 2012 (UTC)

Python example doesn't show distinction between polymorphism and method overriding
The article clearly states that polymorphism is not method overriding. Yet isn't the latter exactly what the example is doing? --Henre (talk) 23:33, 31 March 2013 (UTC)


 * This article clearly states very little! We have:
 * ... Polymorphism (which is strictly referring to subtype polymorphism in the context of this article) ...
 * Followed by a section headed:
 * Parametric Polymorphism
 * If you come to the areticle with no prior knowledge, you would leave confused. --Paddy (talk) 05:49, 21 April 2013 (UTC)