Jump to content

Talk:Polymorphism in object-oriented programming

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SineBot (talk | contribs) at 17:58, 6 February 2010 (Signing comment by Szepi - "Does Perl support polymorphism?: new section"). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
WikiProject iconJava Redirect‑class
WikiProject iconThis redirect is within the scope of WikiProject Java, a collaborative effort to improve the coverage of Java on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
RedirectThis redirect does not require a rating on Wikipedia's content assessment scale.
WikiProject iconComputer science Redirect‑class
WikiProject iconThis redirect is within the scope of WikiProject Computer science, a collaborative effort to improve the coverage of Computer science related articles on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
RedirectThis redirect does not require a rating on Wikipedia's content assessment scale.
Things you can help WikiProject Computer science with:

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)[reply]

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 (talkcontribs) 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)[reply]

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 <Get> 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 <get> data and <send>. 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 <vector>

is the #include <vector> 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)[reply]

I agree, inheritance would be the more natural way to do this. 83.166.176.136 (talk) 14:07, 9 July 2008 (UTC)[reply]
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)[reply]

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)[reply]

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)[reply]

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)[reply]

syntax of the examples

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)[reply]


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 (talkcontribs) 21:18, 11 April 2009 (UTC)[reply]

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)[reply]

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)[reply]

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)[reply]

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)[reply]


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)[reply]

Does Perl support polymorphism?

Consider the following modification of the perl code:

<source lang=perl> {

   package Animal;
   sub new {
       my ( $class, $name ) = @_;
       bless { name => $name }, $class;
   }
   sub talk  { 'HOW???' }

}

{

   package Cat;
   use base qw(Animal);
   sub talk  { 'Meow' }

}

{

   package Dog;
   use base qw(Animal);
   sub talk  { 'Woof! Woof!' }

}

my $a = Cat->new('Missy'); my $b = Cat->new('Mr. Mistoffelees'); my $c = Dog->new('Lassie');

for my $animal ( $a, $b, $c ) {

   bless $animal, "Animal";
   print $animal->{name} . ': ' . $animal->talk;

}

The major difference to the example given in the article is that we "cast" the objects to the type "Animal" (in the line before the print statement). As a result, the output will be:

Missy: HOW??? Mr. Mistoffelees: HOW??? Lassie: HOW???

The example demonstrates that Perl does not achieve dynamic binding through its built-in language constructs. This should be clarified in the text. Probably the same applies to other weakly typed languages (as noted at the beginning of the article). I propose to remove this example. Alternatively, change it to the above one and explain the issue. Then add an example that demonstrates how in Perl one could implement dynamic binding. {{subst:Unsigned|1=Szepi|2=17:57, 6 February 2010 (UTC)}}