File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about polymorphism" Watch "about polymorphism" New topic
Author

about polymorphism

anita narula
Greenhorn

Joined: Nov 28, 2000
Posts: 10
Overloading, Overriding or Late Binding .Which of these are the features of polymorphism?
I have read that overloading is a feature of polymorphism and overriding is a feature of runtime polymorhism?
But different mock exams give different answers. Can anybody give me the correct answer.
Thanks in advance.
David Wake
Greenhorn

Joined: Apr 18, 2001
Posts: 17
Late binding: YES
Overloading: NO
Overriding: Hmmmmm not sure. I'd go for YES but without 100% confidence.
David
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Late binding is the mechanism that allows polymorphism occur. Overriding is the benefit that you get from polymorphism.
Overloading is a nice language feature that allows you to create an object with different signatures, or call a method with different input parameters, but the result is still one object is created or one method is called, it really has nothing to do with polymorphism.
All Polymorphism involves sub-classing or implementing interfaces and the features of these activities.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi,
Cindy, sorry to contradict you but ... overloading is also polymorphism; the lazy, or easy kind

The really easy kind of polymorphism is called overloading in Java and other languages, and it means that in any class you can use the same name for several different (but hopefully related) methods.
The second, more complicated kind of polymorphism, true polymorphism, is resolved dynamically at runtime.

Just Java 2, 4th Edition by Peter van der Linden
I've also seen it referred to as the lazy polymorphism in other text.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Ah Jane, just what I like, philosophical debates over the exact meaning of "true polymorphism" .
Thinking in Java, Bruce Eckel, p321

People are often confused by other non-object-oriented features of Java, like method overloading, which are sometimes presented as object-oriented. Don't be fooled: If it isn't late binding, it isn't polymorphism.

(your turn )
-----------
Cindy Glass
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Cindy Glass (edited April 28, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And Beginning Java 2 by Ivor Horton also says the overloading is not polymorphism. I would say that Peter stands alone in his opinion. Polymorphism can only exist with late binding which has nothing to do with overloading.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
vivek bawge
Greenhorn

Joined: Apr 19, 2001
Posts: 24
hi ,
i dont know how java terminology is different than the world. if not then overloading is indeed polymorphism. i dont know if its in true java terms or not.
but poly is many morphism forms
in overloading we assign different tasks to the same method name depending on the context
so overloading is compile time polymorphism
tell me if i am wrong
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Since in overloading, the methods have different signatures then they can not be said to have the same form and there is no polymorphism. There is no such thing as compile time polymorphism. And this has nothing to do with Java. The GoF book also states that polymorphism only occurs at run time.
[This message has been edited by Thomas Paul (edited April 29, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Thomas ...
If there is no such thing as compile time polymorphism how can overloading occur? If you create two variables with the same name, you get a compile time error. Overloaded methods have the same name, different signatures. It doesn't settle on just one of the methods as valid and give you errors for the others; it allows all of them if the parameter lists are different.
If you tell a dog to 'bark' ... that's a message. You can also say 'bark loud', 'bark 2 times'. The message is the same, with minor modifications. Same message taking different forms. The message is polymorphic
In overloading, the same object responds differently to what is basically the same message.
In overriding, different objects respond in individual ways to the same message. The object takes different forms.
Guess it depends on which side of the mirror your looking through
By the way, Peter isn't entirely alone ... Jaworski states

Polymorphism is the capability to assume different forms. In object-oriented programming, this refers to the capability of objects to have many methods of the same name, but with different types of arguments.

And just to prove I can talk out of both sides of my mouth at the same time see this post http://www.javaranch.com/ubb/Forum24/HTML/009487.html
Jane
[This message has been edited by Jane Griscti (edited April 29, 2001).]
[This message has been edited by Jane Griscti (edited April 29, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Of course since this is a forum on SUN certification, the only correct answer is the answer that Sun expects. In their tutorial they ONLY talk about polymorphism in terms of inheritance and Late Binding.
from Object-Oriented Programming Defined http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava2/oo.html#what

Object-Oriented Programming Defined
Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.

and from http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava2/oo.html#poly

Polymorphism
Another way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship.
For example, if you define a method that takes a java.lang.Object as a parameter, it can accept any object in the entire Java platform. If you define a method that takes a java.awt.Component as a parameter, it can accept any component object. This form of cooperation is called polymorphism.
You saw an example of polymorphism in Part 2, Lesson 5: Collections where a collection object can contain any type of object as long as it descends from java.lang.Object. It is repeated here to show you that Set collection can add a String object and an Integer object to the Set because the Set.add method is defined to accept any class instance that traces back to the java.lang.Object class.

Oh and from a Sun white paper
From http://java.sun.com/docs/white/langenv/Object.doc1.html
3.1 Object Technology in Java

Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message

Note that is says different OBJECTS not different METHODS.
[This message has been edited by Cindy Glass (edited April 30, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If there is no such thing as compile time polymorphism how can overloading occur?
Because overloading has nothing to do with polymorphism!
In overloading, the same object responds differently to what is basically the same message.
Basically the same message? The messages are different therefore there is no polymorphism involved. Here are two methods:
public void calcTotal(int i);
public void calcTotal(Vector v);
How can you say that a Vector and int are "bascially the same message"? In fact the methods may have nothing to do with each other and the name being the same is just an accident.
Check the GoF book. Page 14 states quite clearly that polymorphism can only occur with dynamic binding.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You saw an example of polymorphism in Part 2, Lesson 5: Collections where a collection object can contain any type of object as long as it descends from java.lang.Object. It is repeated here to show you that Set collection can add a String object and an Integer object to the Set because the Set.add method is defined to accept any class instance that traces back to the java.lang.Object class.

This is completely wrong!!! Monica Pawlan has come up with a brand new definition of polymorphism that has nothing to do with overloading or overriding! Ms. Pawlan is basically saying this:
The add method of Collection can take any Object. Therefore since there are more than one type of Object, add demonstrates polymorphism. Sorry, Ms. Pawlan but you are wrong.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I am such a dope sometimes... I meant to delete my own post and I accidentally deleted Cindy's. Sorry Cindy.
Here is part of Cindy's original post quoting from a Sun tutorial:
http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava2/oo.htm l#poly

Polymorphism
Another way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship.
For example, if you define a method that takes a java.lang.Object as a parameter, it can accept any object in the entire Java platform. If you define a method that takes a java.awt.Component as a parameter, it can accept any component object. This form of cooperation is called polymorphism.
You saw an example of polymorphism in Part 2, Lesson 5: Collections where a collection object can contain any type of object as long as it descends from java.lang.Object. It is repeated here to show you that Set collection can add a String object and an Integer object to the Set because the Set.add method is defined to accept any class instance that traces back to the java.lang.Object class.
String custID = "munchkin";
Integer creditCard = new Integer(25);
Set s = new HashSet();
s.add(custID);
s.add(creditCard);

[This message has been edited by Thomas Paul (edited April 30, 2001).]
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3336
    
    8
Interesting....this is what I found at www.dictionary.com :

polymorphism
A concept first identified by Christopher Strachey (1967) and developed by Hindley and Milner, allowing types such as list of anything. E.g. in Haskell:
length :: [a] -> Int
is a function which operates on a list of objects of any type, a (a is a type variable). This is known as parametric polymorphism. Polymorphic typing allows strong type checking as well as generic functions. ML in 1976 was the first language with polymorphic typing.
Ad-hoc polymorphism (better described as overloading) is the ability to use the same syntax for objects of different types, e.g. "+" for addition of reals and integers or "-" for unary negation or diadic subtraction. Parametric polymorphism allows the same object code for a function to handle arguments of many types but overloading only reuses syntax and requires different code to handle different types.

Jane, your example of bark does not really prove that overloading is polymorphism. The whole point of polymorphism is that the caller does not know what's actually going to happen when a "polymorphic" method is called. It depends on the actual object. In your example, the caller "knows" that bark(), bark(int loudness) are two diferent properties of Dog and is he/she knows what will happen.
On the other hand, if you call bark() on an Animal, you do not know whether you are going to hear a Dog's bark or your boss's yelling!!!
I do not think that overloading is anyway related to polymorphism.
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Try out the world's only WebCompiler!
www.jdiscuss.com


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I wondered about that . I am just glad that it is not a re-occurrance of the dread plaque of a couple of weeks ago.
And Thomas, if you have
myMethod(Object x){
}
And x can be any sub-class of Object - that is EXACTLY what polymorphism is all about.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Cindy Glass:
Oh and from a Sun white paper
From http://java.sun.com/docs/white/langenv/Object.doc1.html
3.1 Object Technology in Java
Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message
Note that is says different OBJECTS not different METHODS.

Now this is the classic definition of polymorphism. The same message sent to two different objects. Which object is getting the message is determined at run time (dynamic binding) which results is polymorphism.
From JavaWorld: http://www.javaworld.com/jw-04-2001/jw-0413-polymorph.html
Both coercion and overloading are classified as ad hoc because each provides polymorphic behavior only in a limited sense. Though they fall under a broad definition of polymorphism, these varieties are primarily developer conveniences. Coercion obviates cumbersome explicit type casts or unnecessary compiler type errors. Overloading, on the other hand, provides syntactic sugar, allowing a developer to use the same name for distinct methods.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3336
    
    8
A message is the data that is sent from the caller to the callee.
In overloading the messages are different (method name is just one part of the message), so there is no reason to not to believe that the action taken by the calleed will be different.
But in overridinging, the messages are exactly the same. The data that is sent from the caller is exactly the same, still the result is different. And that's what polymorphic behavior is.
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Try out the world's only WebCompiler!
www.jdiscuss.com
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Cindy Glass:
And Thomas, if you have
myMethod(Object x){
}
And x can be any sub-class of Object - that is EXACTLY what polymorphism is all about.

Actually, no. Since method binding can be done at compile time, there is no polymorphism. Note the definition from the Sun site:

Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message

This is the same message sent to the same object. Now if myMethod does something like run the equals method of x, since x might have overridden object's equals method, there is no way for the compiler to know what equals method to run so it must be determined at run time. That is polymorphism!
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Thomas,

public void calcTotal(int i);
public void calcTotal(Vector v);
How can you say that a Vector and int are "bascially the same message"? In fact the methods may have nothing to do with each other and the name being the same is just an accident.

Thomas, it is the same message ... calcTotal ... the implementation may be totally different but the meaning is the same .. some type of total is to be calculated. That is, if you assume the method naming has some relevance to what's required.
Paul,

Jane, your example of bark does not really prove that overloading is polymorphism. The whole point of polymorphism is that the
caller does not know what's actually going to happen when a "polymorphic" method is called. It depends on the actual object.
In your example, the caller "knows" that bark(), bark(int loudness) are two diferent properties of Dog and is he/she knows that will happen.

Point I was trying to make is that the same message can have different forms for the same object vs different objects reacting to the same message.
Cindy, Did you read my other post? You quoted all the same sources
I agree that overriding implemented with dynamic binding is pure polymorphism. However, I still think overloading is a form of polymorphism.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3336
    
    8
Jane, I think you missed the point about "message". As I said, method name is not the whole message. It is just a part of it. A message includes method name plus parameters. And that's why calling overloaded methods means sending different messages. And that's why I think it is not (no kind of, true or otherwise) polymorphism.
I mean, what kind of polymorphism is this anyway? I am sending different messages and getting different results!!!
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Try out the world's only WebCompiler!
www.jdiscuss.com
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Paul,
The intent of the message is the same; with minor modifications. You want the dog to bark. If overloading is not polymorphic you would have to write barkLoud, barkTwice, etc. With overloading you can summarize your intent with one 'verb' and then tack on 'adverbs'. Simplifies things.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Jane Griscti:
The intent of the message is the same; with minor modifications. You want the dog to bark. If overloading is not polymorphic you would have to write barkLoud, barkTwice, etc. With overloading you can summarize your intent with one 'verb' and then tack on 'adverbs'. Simplifies things.

But the methods could be anything:
public void method1(int i);
public void method1(Button b);
How is this polymorphism?
According to JavaWorld:
"Both coercion and overloading are classified as ad hoc because each provides polymorphic behavior only in a limited sense. Though they fall under a broad definition of polymorphism, these varieties are primarily developer conveniences."
So in a limited sense, overloading could be thought of as polymorphism but according to Sun's defintion, "the same message sent to different objects..." it is not. Overloading is different messages sent to the same object.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If overloading is not polymorphic you would have to write barkLoud, barkTwice, etc
Why? A method is defined by the combination of name and parameters so two methods with the same name are not the same if they have different paramters.
public class A {
public void method1(Vector v) {}
}
public class B extends A {
public void method1(int i) {}
}
So is this polymorphism?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I'm beginning to think this topic belongs in meaningless drivel.
To go back to the original post:
"Can anybody give me the correct answer."
The answer is: "NO! No one can give you the correct answer."
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
So the result is:
Overriding is PURE polymorphism
Overloading is "sort of" polymorphism
If you are taking a Certification exam stick with PURE polymorphism as put forth by Sun.
PS
Jane I obviously didn't read your post WELL ENOUGH . Or I could just say "great minds think alike" .
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
LOL ... I think Thomas is right.
Anita, apologies if we muddied the water for you or any other Rancher!
Here's my attempt to clear things up:

  • Java implements polymorphism using overriding which is made possible thru dynamic or runtime binding
  • overloading, while considered by some to be lazy polymorphism, is a feature of the Java language but not the mechanism used by Java to implement polymorphism

  • Bottom line, if you get any exam questions that ask which best describes how polymorphism is implemented in Java ... choose overriding
    Think we all agree on that one
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
    [This message has been edited by Jane Griscti (edited May 01, 2001).]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20729
    ∞

There is ample proof that polymorphism was around before there was such a thing as a "class". However, I think this is contraray to modern OO - so I think we should ignore it.
We need to form legions of bigotry. We need to poo-poo people who beleive that overloading is polymorphism. Through denial, we can change history and make things right! If you hear someone say that overloading is polymorphism, slap your hands over your ears and shout "LA LA LA LA LA LA LA ...."
This world is only big enough for people with stars upon thars!

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Wow, a topic brought back from the dead. I had to re-read the whole thing to recall what I was thinking.
Paul, language changes (I mean the English language) and what was meant by a word can change over time. The formal definition of polymorphism in an OO language would not include over-loading. What it means in non-OO languages may be something else.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Paul Wheaton:
We need to form legions of bigotry. We need to poo-poo people who beleive that overloading is polymorphism.
Since this is the Sun certification forum, we go by what Sun puts on their site.
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
Next somebody is going to ask whether Java passes by value or by reference...
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Wow Paul ... were you extremely bored this morning or did your satellite modem finally show up and it's signals are being bounced off Mars or Venus to put you in a time warp
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Cindy Glass:
All Polymorphism involves sub-classing or implementing interfaces and the features of these activities.

In statically typed languages like Java, C++ etc. - yes.
In dynamically typed languages like Smalltalk, Ruby etc. - no. Here it only requires identical method signatures.


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about polymorphism