Talk:Polymorphism in object-oriented programming: Difference between revisions

Content deleted Content added
Trying to understand the first sentence of the article.
Line 97:
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. [[User:Grauw|Grauw]] ([[User talk:Grauw|talk]]) 00:19, 13 January 2010 (UTC)
 
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.
== Does Perl support polymorphism? ==
 
ConsiderHere is the followingfirst modificationsentence of the perl codearticle:
 
''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.
<source lang=perl>
''
{
package Animal;
sub new {
my ( $class, $name ) = @_;
bless { name => $name }, $class;
}
sub talk { 'HOW???' }
}
 
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):
package Cat;
use base qw(Animal);
sub talk { 'Meow' }
}
 
''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.
{
''
package Dog;
use base qw(Animal);
sub talk { 'Woof! Woof!' }
}
 
Still, I do not really like this sentence.
my $a = Cat->new('Missy');
What does it mean to create a variable that has more than one form?
my $b = Cat->new('Mr. Mistoffelees');
What is the form of a variable? Same problem with a function.
my $c = Dog->new('Lassie');
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''?
for my $animal ( $a, $b, $c ) {
What does it mean for the object to have a single form? What is a form? Type?
bless $animal, "Animal";
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.
print $animal->{name} . ': ' . $animal->talk;
[[User:Szepi|Szepi]] ([[User talk:Szepi|talk]]) 04:49, 5 February 2011 (UTC)
}
</source>
 
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). Also note that additional talk method added to package Animal.
As a result, the output will be:
<blockquote>
Missy: HOW???
Mr. Mistoffelees: HOW???
Lassie: HOW???
</blockquote>
 
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. <small><span class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:Szepi|Szepi]] ([[User talk:Szepi|talk]] • [[Special:Contributions/Szepi|contribs]]) 17:57, 6 February 2010 (UTC)</span></small><!-- Template:Unsigned --> <!--Autosigned by SineBot-->
 
== Polymorphism is not exactly what this article claims it is ==