This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
To my understanding, polymorphism means overloading. However, I have seen people relate polymorphism to inheritance. I am very confused and need help in clarifying this concept.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Polymorphism:
-------------
InterfaceI obj;
obj = new A();
obj.doSomething(10, 20) returns 200
obj = new B();
obj.doSomething(10, 20) returns 30
Overloading:
------------
A a = new A();
a.doSomething(10) returns 100
a.doSomething("10") returns 500
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
See your answer here with tons of examples:
Methods Overloading and Overriding
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Polymorphism is overriding, not overloading although you will find plenty of infidels who say otherwise.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Copied one from the link most close to your question:
Q. What are the differences between overloading and overriding. Are they both concepts of polymorphism?
A: No, overloading is NOT a concept of polymorphism.
Let me try a short one about overloading. The approach is like you take the test, cross out something obviously not correct, before you decide what is correct.
3 + 5
3.0 + 5.0
The plus(+) operator is overloaded long before even OO concepts came into play in computer sciences. It is in Fortran ( the first high level computer language) for sure.
1) American drives on the right. British people drives on the left.
2) Maha Anna is right on this topic.
3) I'll be right back.
4) These are right angle triangles.
5) You have the right to disagree with me.
6) He is a right extremist.
7) ...
The English word right is overloaded here. All natural languages are overloaded long before computer even invented.
Polymorphism is a very specific OO concepts, which has nothing to do with overloading. You really need to read a good OO book to understand it. It is too large a job for me to write about it here.
There are so much misunderstanding out there on this topic, a lot of them are in published books. Like Marcus said: "Don't believe everything you read."
If you don't believe what you read here by Roseanne, I'm OK.
SCJP Questions & Answers by Roseanne Zhang
[ May 26, 2002: Message edited by: Roseanne Zhang ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Originally posted by Thomas Paul:
Polymorphism is overriding, not overloading although you will find plenty of infidels who say otherwise.

My understanding is that "Polymorphism" means "many forms" ans in this reading it can embrace overloading as well, why not (one method for many kinds of parameters). From my personal theoretical experience, I found the concept of "polymorphism" false, misleading, telling nothing, I regret I spent time trying to figure out what it means. It's an epiphenomena, mental category, scholastic concept like "how many Angels can survive on the top of the needle" (sorry for bad translation). there are real things, and there is "polymorphism" :roll:
I would be happy to hear counter-opinions from people with real OOP experience who would claim that "polymorphism" helped them to understand... at least something :roll:
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I think it was Peter van der Linden who first said that overloading is an example of shallow polymorphism.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
If someone want to mislead or to be misleaded, that is your choice! Feel free to do so.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Here is the official Java defintion:
Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message.
Same message - different objects -- In other words, overriding not overloading.
As I said, you can find plenty of people that will call overloading a form of polymorphism. They are wrong and should be ignored.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Thomas is very correct. I still want to add a little But's...
Even Java can say/use polymorphism, but polymorphism was not invented by Java. Find any good OO book before Java, read it carefully. You will know overloading is NOT polymorphism.
Java is not the beginning of the world, neither the end of the world. I remember a Java book even try to redefine "dereferencing" because Java does not support dereferencing...
"Jin xing shu, bu ru wu shu."
This is famous a Chinese saying, by someone might be Confucius (not sure). Meaning is pretty close to what I quoted by Marcus above.

"If you believe everything on books, then you might be better off without books."
[ May 24, 2002: Message edited by: Roseanne Zhang ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Ok. In your everyday talking, how often do you use terms like "derieved class, interface, overriden method" etc. and any of patterns, vs. polymorphism?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Ok. In your everyday talking, how often do you use terms like "derieved class, interface, overriden method" etc. and any of patterns, vs. polymorphism?
Since my everyday talking involves designing a rules engine framework with 3 senior software engineers and a data architect, I would say 5 out of every 7 days on average.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
The most wordy place about poymorphism I found was http://www.ergoit.co.uk/index.html but it seems down now
Anyway, this is from files I saved:
"polymorphism : a fancy word, but well worth knowing.
Polymorphism means many forms (it derives from the Greek words Poly, meaning many, and Morphos, meaning form). A book is a book, whether paperback, hardback, small or large. One name, but many forms. Our ability to allow one concept to embrace many variations makes life easier. So it is with the Object Oriented concept of polymorphism. Rather than a proliferation of similarly named methods, for example, we can have the same method name, just called with different parameters to do slightly different things. Like calling the Rectangle class constructor with coordinates for opposite corners, or with one corner plus the width and height. They both achieve the same, so deserve the same name, but one form of the constructor suits one situation, another may suit another situation. This is just one example of polymorphism, which is detailed below, along with others.
Then there are three files that I list bold and sections within each:
Polymorphism with variables
Object variables can hold any lower level class object
One method - different value types for parameters
Polymorphism across classes
Same method across different classes
Interfaces : standardising common methods
Polymorphism within a class hierarchy:
One class with multiple versions of the same method name
One class with multiple constructors
Child method variations on a parent class method
At this point I decided that there are just too many things that can be called "polymorphism" for the word to be of anything useful.
[ May 24, 2002: Message edited by: Mapraputa Is ]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Just added an empty Operation Research Glossary tonight into my
(no password required).
Ahh, I'd better not to add an OO glossary here. I've absolutely no interest to debate on this topic anymore.
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
Thomas , Roseanne might like to ignore this , because i am from the camp who believe overloading IS A form of polymorphism.
quote from a OOP's book by Timothy Budd :
" Pure Polymorphism accurs when a single function can be applied to arguments of a variety of types. In pure polymorphism, there is one function( code body) and a number of interpretations . The other extreme occurs when we have a number of functions ( code bodies) all denoted by the same name-a situation known as overloading ( or sometimes as ad-hoc polymorphism ) .Between the two extremes are overriding and deferred methods "
If i have to put it in very raw form i 'll say Overloading is compile time polymorphism whereas Overriding is runtime polymorphism .


Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Thomas Paul:

Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message.
Same message - different objects -- In other words, overriding not overloading.
As I said, you can find plenty of people that will call overloading a form of polymorphism. They are wrong and should be ignored.

I think I hold with the idea of 'shallow' polymorphism. Overloading I can see as a kind of word variant to the original message. When we consider that overloaded methods can indeed appear in subclasses as supplments to the original method, it seems reasonable enough to me.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Overloading can't be polymorphism because you're not talking about sending the same message, ie, calling the same method. You're calling different methods. They share the same symbolic name as a convenience to the programmer, but they are completely different methods. To the dispatcher:
void foo(int a);
and
void foo(int a, String s);
might as well be
void chewbaca(int a);
void dilbert(int a , String s);
Overridden methods however, are the same message, and it's up to the runtime dispatcher to determine which object gets the message based on the runtime type of the instance on which you're invoking the method.
I think overloading should be considered syntactic sugar which simplifies your symbol table so you don't end up with:
String s = new StringFromString(anotherString);
String s1 = new StringFromStringBuffer(aStringBuffer);
etc.
It's definitely not polymorphism.
[ May 25, 2002: Message edited by: Rob Ross ]
[ May 25, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
Hi guys,
I believe in this statement given by Patrick Naughton,one of the founding member of the original Sun Microsystems project team who developed Java.He says,
"Method Overloading is one of the ways that Java implements Polymorphism".
I agree with Maraputa's statement..

My understanding is that "Polymorphism" means "many forms" ans in this reading it can embrace overloading as well, why not (one method for many kinds of parameters).

Bye,
Viki.


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
http://research.microsoft.com/Users/luca/Papers/OnUnderstanding.pdf :

As is implicit in the choice of names,
universal polymorphism is considered true polymorphism, while ad-hoc polymorphism is some kind of
apparent polymorphism whose polymorphic character disappears at close range. Overloading is not true
polymorphism:
instead of a value having many types, we allow a symbol to have many types, but the values
denoted by that symbol have distinct and possibly incompatible types.


"..In overloading the same variable name is used to
denote different functions, and the context is used to decide
which function is denoted by a particular instance
of the name. We may imagine that a preprocessing of the program
will eliminate overloading by giving different
names to the different functions; in this sense overloading is
just a convenient syntactic abbreviation.
"
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Michael Ernest:

I think I hold with the idea of 'shallow' polymorphism. Overloading I can see as a kind of word variant to the original message. When we consider that overloaded methods can indeed appear in subclasses as supplments to the original method, it seems reasonable enough to me.

I have to admit that I haven't a clue what you are talking about. Imagine class A with method A1(). Now imagine child class B with method A1(). That's polymorphism. Now add A1(string s) and A1(int i, double d). Since they have nothing to do with the original A1 other than a shared name, they are not polymorphism. They are coincidentally named methods. They could be called B1(string s) and B1(int i, double d) and the difference would be nil. Overriding is a useful technique that makes all sorts of coding practices possible. Overloading is just because we are too lazy to try to remember a bunch of different method names.
As I said earlier, you can find plenty of people who will say that overloading is polymorphism or "a form of" polymorphism. They are wrong.
Polymorphism = same message, different methods.
Overloading = different message, different methods.
Overloading is not polymorphism.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Ladies and gentlemen,
Polymorphism (as an OOP concept) is a product of human activity, which means its definition cannot be other than conventional. It means whatever we agree it means. It's perfectly possibly for one authority or one part of community to intend it in one sense and for another authority (another part of community) in another. Theorist of OOP mean one thing, Java implementation may narrow (or not) the meaning, Peter van der Linden is in his full right to stretch the term in any direction. Thus, we see that the term "polymorphism" is itself polymorphic
To make the term unambiguous we need a standard that would define its meaning, and as far as my knowledge goes, we do not have any.
In such circumstances, it makes little sense to me to argue what is polymorphism and what it is not. The harder we will try, the more confusion will be outputted. Look, besides "plain polymorphism" we already got "true polymorphism", "compile-time polymorphism" and even "shallow polymorphism"! What's next? "Soft polymorphism"? "Weak polymorphism"? :roll:
Having said that... I do not meet this term too often. It is used to teach OOP to newcomers and it seems it has limited usage after that. Even in period of installing OOP in my brain, I found this term more misleading than useful. It helps if new things are explained by comparing them to something already known, not by attributing it to obscure abstract category whose definition is vague for you.
When they tell you about method overriding and say "it's an example of polymorphism"... Well, whatever you call it...
Therefore, the only "real" thing about the "polymorphism" term is how it is actually used. I made a little research and searched for "polym*" in PDF books I have or checked index for "polymorphism", "polymorphic" for "paper" editions. Here we are.
Ted Neward. Server-Based Java Programming - 0 results
Joshua Bloch. "Effective Java" - no index entry for "polymorphism", "polymorphic"
M.Fowler, UML distilled - no index entry for "polymorphism", "polymorphic"
However,
James W. Cooper. The design patterns, Java companion returns three results.
"Thus, while the Iterator or Enumeration interface is that is intended to be polymorphic, this is not directly possible in Java."
"This says that any concrete Visitor classes we write must provide polymorphic visit methods for both the Employee and the Boss class."
"The Visitor calls the polymorphic accept method of a given object, and the accept method calls the polymorphic visit method of the Visitor."

M.Fowler. "Enterprise Application Architecture", online edition. 3 out of 30 saved by Map files have entries for "polymorphism"
"You can use static find methods, but that precludes polymorphism should you want to substitute different finder methods for different data sources."
"Keeping them separate can make polymorphic referneces much more awkward."
"You can't have direct instance to instance relationships, and polymorphism does not work well."

From this I am making the following conclusions: 1) "polymorphism is used to designate "method overriding", 2) the most popular usages are "polymorphic method" (which means method that can be overridden?) and "polymorphic reference" (aren't all references potentially polymorphic, unless the class is final?)
"Polymorphic method" is IMHO ambiguous and if you do not believe it read this thread again "You can use static find methods, but that precludes polymorphism" can be more precisely formulated as "You can use static find methods, but that precludes the method from being overriden"
"Polymorphic reference" sounds Ok to me (that means I didn't find equally short but more precise variant ), but it has nothing to do with our overriding/overloading confusion
Thank you for your attention.
[ May 25, 2002: Message edited by: Mapraputa Is ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
P.S. http://research.microsoft.com/Users/luca/Papers/OnUnderstanding.pdf :
1.3. Kinds of polymorphism
Figure 1.


[ May 25, 2002: Message edited by: Mapraputa Is ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If polymorphism means whatever we want it to mean then the word serves no useful purpose. But the word serves a very useful purpose. When I am in a technical discussion I want to use words like polymorphism and singleton and proxy to mean specific things. That is why I am so strongly opposed to this incorrect usage of the term. Polymorphism is a key OO concept. It is made possible by overriding but it is not overriding.
"polymorphic reference" (aren't all references potentially polymorphic, unless the class is final?)
Depends on the language. In C++ and C#, only virtual methods are potentially polymorphic.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Thomas Paul:
If polymorphism means whatever we want it to mean then the word serves no useful purpose.

I think nobody ever suggested something like that. Or does someone aggree that polymorphism possibly could mean "being green and ugly"?
But the word serves a very useful purpose. When I am in a technical discussion I want to use words like polymorphism and singleton and proxy to mean specific things.

So did you ever encounter big problems because "proxy" can also be used to describe a program for shielding web access or a class from the java.lang.reflect package or or a proxy person or a juristic procuration? Or was your audience able to understand that you in fact meant the proxy *design pattern*?
That is why I am so strongly opposed to this incorrect usage of the term.

I am curious: Where does your "correct" definition of polymorphism come from?
[ May 26, 2002: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
In OO speak polymorphism refers only to abstraction and inheritance. Abstraction is actually a method of creating functionality and extensability.
Parametric polymorphism is a tricky one. In C++ at least it is all resolved at compile time and you executable grows with each diffent use, so it's really just a shortcut. An implementation could be written to resolve at runtime though. Java 1.5 may well include such an implementation...
Overloading is just a shortcut, plain and simple. The JVM doesn't care what you name your methods. Hell, it probably can't even speak English. A once-over with an obfuscator can clear up that overloading for you (except in Java constructors) without decreasing functionality at all.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
From here
Languages (natural and programming languages included) are not rigorous science, a lot of human factors are involved. To programming languages, another factor is the cost.

The word Polymorphism itself in natural languages can be overloaded too like the Right example in English in my above post. You can overload the word polymorphism with whatever meaning you want it to mean.
However, I still agree with Thomas Paul or most computer scientists' narrow and precise definition of polymorphism. No more arguments are needed, we all can rest in peace...
Roseanne
[ May 26, 2002: Message edited by: Roseanne Zhang ]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Just added a few more overloaded right on my above post. Actually, you can even overload the English word right to mean a dog in certain communication circle. Just thinking a scenario, you name your dog as Right, then in your family, when you or your kids talk about Right, the most of the time, it is referring to the dog.
And it is a valid overloading, no body will put you in jail or even criticize you for your use of English word right this way...
[ May 26, 2002: Message edited by: Roseanne Zhang ]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
When English word is overloaded, we understand the difference by the context. Compiler understands the method overloading by the signature, which serves the same purpose as context, but more reliable, since compiler is not as intelligent as we are.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Comedians use natuaral language overloading a lot, no matter in what languages. At least I know that they are heavily used in both English and Chinese. Since context can be ambiguous, which can be very funny in certain cases.
[ May 26, 2002: Message edited by: Roseanne Zhang ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
I do not think calling overloading "syntactic sugar" really explains the difference. In the final account, only 0s and 1s are real, all other is "syntactic sugar". Our computers are Turing machines, in all its glorious primitiveness.
As Luca Cardelli (thanks for the link, Rob! ) penetratingly noticed, all universe can be represented by sets. There is nothing besides sets, or, better, there is no need for anything besides sets.
"In set theory, everything is either an element or a set of elements and/or other sets. To understand how untyped this universe is, one must remember that most of mathematics, which is full of extremely rich and complex structures, is represented in set theory by sets whose structural complexity reflects the complexity of the structures being represented. For example, integers are generally represented by sets of sets of sets whose level of nesting represents the cardinality of the integer, while functions are represented by possibly infinite sets of ordered pairs with unique first components."
Therefore, "true" polymorphism has to have its roots in set theory, in some base characteristic of sets. What characteristic can it be? There aren't too many to choose from. Cardinality - that's it. "True", or "universal" polymorphism works on an infinite number of sets, "false" - on finite.
"Universally polymorphic functions will normally work on an infinite number of types (all the types having a given common structure), while an ad-hoc polymorphic function will only work on a finite set of different and potentially unrelated types."
Now the connection with concept of "run-time" is interesting. "Compile-time" polymorphism cannot work on infinite number of types because time is limited - compilation must finish. Of course, interpreted languages blur this distinction also...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I am curious: Where does your "correct" definition of polymorphism come from?
Thomas is quoting the definition used by Gosling and McGilton in The Java Language Environment white paper (1996). I think it's an overstatement to call it the "official" definition however, as the term is not used consistently elsewhere, and is never defined in, say, the JLS, the tutorial, or even this nifty glossary from Sun. While the white papers might have been expected reading for anyone learning about Java when it first came out, that's not the case today - and if Sun had an "official" definition of polymorphism, they could have put it somewhere more likely to be seen by people.
Ultimately, whenever you hear the word "polymorphism" now you must consider the possibility that the author or speaker is using the term in a different manner than what's accepted by the majority of OO theorists. It's generally not too difficult to work out whether overloading is considered to be included, in a given context. It's not unlike the situation with a term like "inner class", which (unlike "polymorphism") does have an official Java definition, yet is nonetheless used incorrectly by a large number of otherwise intelligent people. :roll: On the one hand, it would be better for those people to learn to use the correct definition - but on the other, the misuse is so prevalent that even those who know the correct definition must also be aware of the false one, and be prepared to a bit of mental translation when speaking to philistines.
[ May 26, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Originally posted by Jim Yingst:
on the other, the misuse is so prevalent that even those who know the correct definition must also be aware of the false one, and be prepared to a bit of mental translation when speaking to philistines.

You're right on the point!! Actually, I'm seriously considering to modify my answer a bit to cover the possibility of the interviewer might be on the other side. This is a benefit I got from the above debate.
[ May 26, 2002: Message edited by: Roseanne Zhang ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Carlos Castaneda coined the term "a sorcerer's controlled folly".
"Since a man of knowledge "sees" and he knows that nothing is more important than anything else then nothing matters to him, he has only his controlled folly, acting as if it mattered even though he knows it does not."
[ May 27, 2002: Message edited by: Mapraputa Is ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism
 
Similar Threads
Polymorphism
MasterExam OO question
Interfaces
instance variables - polymorphism(overriding)
Polymorphism