wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes An example of polymorphism is Overloading(?) Overriding(?) 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 "An example of polymorphism is Overloading(?) Overriding(?)" Watch "An example of polymorphism is Overloading(?) Overriding(?)" New topic
Author

An example of polymorphism is Overloading(?) Overriding(?)

Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Jaworski "Java 2 Certification Training Guide" Mock Exam:
85. Which is an example of polymorphism?
Select any answer that is right
A. Inner classes
B. Anonymous classes
C. Method overloading
D. Method overriding

Jaworski's answer is C only.
Jaworski, "Polymorphism" p.136: "Polymorphism is the capability to 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. The print() and println() methods of the PrintStream class are an excellent example of of polymorphism. These methods support printing of objects of different types.
The compiler and runtime system support polymprphism by matching each method invocation to the form of that method. The capability to figure out which method to use in complex situations is the essence of polymorphism."
Jaworski, "Overloading Methods" p. 149: "Method overloading is the primary way in which polymorphism is mplemented in Java."
But, in my own words, as I understand it now, polymorphism is when you have one method take different behaviour, depending on the current object instance type, subclasses of a generic class.
Such as as for the follwoing example (from Thinking in Java p.660):

... and so on...
The intent is for the client programmer to call draw() through a generic Shape reference. draw() uses the println method to display the object string representation, automatically calling the toString() method of the object. And the toString() method is overriden in in all of the derived classes, and even though it is called through a generic Shape reference. That's polymorphism. cf p. 660-661.
So, I think the answer should be D only.
Could it be C and D?
Could anyone bring some light on this, please?
Please provide references too, if possible.
Thanks.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
The correct answer is D only.
It is a typo. Please read the related discussion in the Errata forum.
Ajith
[This message has been edited by Ajith Kallambella (edited November 07, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Thanks, Ajith.
Although according to the text extract from Jaworski's book that I put in this topic, I don't think this is a typo. He really meant it!
TARIT DASGUPTA
Greenhorn

Joined: Nov 05, 2000
Posts: 14
Hi Serge,
Polymorphism means execution of differnt methods(with same name) depending on the signature of the method. The signature of a method depends on name, no. of parameter, type of parameters, order of parameters.
Unlike C++, in Java we can only have overloading by having differnt forms of the function in a class.
Over-riding is not at all polymorphism.
Ajith please put some light on this if I am wrong.
Thanks
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Tarit,
I'm afraid you are wrong.
The following is the webopedia definition for Polymorphism -
Polymorphism means the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different circumference methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the circumference method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language (OOPL).
The type of polymorphism described above is sometimes called parametric polymorphism to distinguish it from another type of polymorphism called overloading.
Though people have been successfuly in calling overloading as polymorphism, only parametric polymorphism qualifies as true polymorphism. Here, method call is resolved at runtime( aka late binding ) unlike overloading where calls are resolved at compile time.
Hope that helps,
Ajith
[This message has been edited by Ajith Kallambella (edited November 08, 2000).]
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Hi Ajith,
Hope you won't hit me on my fingers!
Sounds like what I read at http://webopedia.internet.com/TERM/p/polymorphism.html
but the article finishes with "The type of polymorphism described above is sometimes called parametric polymorphism to distinguish it from another type of polymorphism called overloading."
Now, I don't want to start a fight with anybody here. I really like this community. I'm only seeking for really understanding the principles. That's all. There really seems to be confusion about this topic.
Is there any chance that somebody could check with what Sun says about this? After all it's them who will mark SCJP participants!
Thanks, and I hope I did not offend anybody.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Firstly I apologise for not giving credits to Webopedia. If you thought I wrote the definition, I'm flattered
It appears to me different OO languages define polymorphism in different ways. For some Overloading is a form of polymorphism and for others only overriding defines polymorphism.
Read the Sun's definition of Polymorphism. Though it doesnot specify about overloading, note that the explanation given hovers around the premises of inheritance, subclassing and runtime binding. Since overloading can be demonstrated in the absence of inheritance, I am inclined towards saying overloading is not (true) polymorphism.
Also read the following -
OO FAQ section about Polymorphism which substantiates my argument in a language-neutral perspective.
OO FAQ . You will need to scroll down to the section 1.14) What Is Specialization/Generalization/Overriding?.
Ofcourse if there is something you don't agree with, come back and I'll be happy to accept contesting viewpoint.
Ajith
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Ajith,
Thanks a million! Now I know why you are sheriff and me ranch hand!
The refenreces that you put forward are totally convincing.
Thanks again for your hard work and patience.
 
wood burning stoves
 
subject: An example of polymorphism is Overloading(?) Overriding(?)
 
Similar Threads
Polymorphism
Jaworski mock - an example of polymorphism (overriding or overloading?)
Polymorphism
Question about polymorphism.
RTTI - what does this mean