wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Sandra Marti
Ranch Hand

Joined: Jun 08, 2000
Posts: 63
The following question is from Jaworski,
Which is an example of Polymorphism?
1.Inner class
2.Anonymous class
3.Overloading
4.Overriding
The answer given is 'Overloading'.
Shouldn't it be overriding???
Basu Patel
Ranch Hand

Joined: May 28, 2000
Posts: 60
The answer is correct, overloading is a perfect implementation of polymorphism considering 'one interface,multiple methods' paradigm.
Overriding does not support the above paradigm. Infact overidding is more like a douplication or carbon copy of the base method, and does not support 'one interface , multiple methods' paradigm.
Basu


<BR>Contact Me<BR> <A HREF="mailto:basu_patel@usa.net" rel="nofollow">basu_patel@usa.net</A> <P>
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well maybe there are other definitions of polymorphism out there, but I always understood it referred to method having more than one implementation. Overloaded methods don't fit this bill - they are essentially separate methods, and would need to be declared separately in in interface in order for that interface to be useful to access the overridden methods. Overridden methods, on the other hand, do fit the bill. A single method declared in an interface can be used to access multiple implementations, depending on the actual class of each object which implements the interface.
Drat. I don't have time right now to track down references with "official" definitions of polymorphism, but I really think Jaworski's answer would be widely considered wrong here. Hopefully others can help here.


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

Joined: Nov 22, 2008
Posts: 18944
The answer "Overloading" is correct.
thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
I agree with Jim. I have always associated polymorphism with dynamic method look-up that occurs with overRIDING methods.
mantri srinivas rao
Greenhorn

Joined: Aug 01, 2000
Posts: 2
There are are two types of polymorphism static and dynamic.
According to static polymorphism will take place at compile time
so overloading is the solution for it.With overloading u can perform polymorphism considering 'one interface,multiple methods' paradigm.
According to dynamic polymorphism will take place at run time so
overriding is the solution for it.With overriding, the methods declared in an interface can be used to access multiple implementations,depending on the actual parent class of each object which implements the interface.
To my knowladge: both 3 and 4 are correct
john shen
Greenhorn

Joined: Jul 23, 2000
Posts: 14
the exam question answer is wrong. you can read the entire chapter of Polymorphism in Bruce Eckel's book. compile-time dispatching is not considered polymorphism in OOP. here is a quote from the summary of that chapter:
Polymorphism means �different forms.� In object-oriented programming,you have the same face (the common interface in the base class) and different forms using that face: the different versions of the dynamically bound methods.
Prasanna Joshi
Greenhorn

Joined: Apr 25, 2000
Posts: 11
Overloading and overriding are both examples of polymorphism.
But Overriding is a perfect example of Runtime Polymorphism.
An example can illustarte this
interface A
{
public void method();

}
class B implements A
{
public void method()
{
System.out.println("Method of A");
}
}
class C implements A
{
public void method()
{
System.out.println("Method of C");
}
}
public class Test
{
public static void main(String[] args)
{
A a ;
B b = new B();
C c = new C();
// now see this
a = b;
System.out.println(a.method());
//will print method of A
now a = c;
System.out.println(a.method());
// will print method of C
}
Hence depending upon the object the methods are invoked

}
}
Sandra Marti
Ranch Hand

Joined: Jun 08, 2000
Posts: 63
Thanks everyone.
I read all the replys and even previous threads on the same question and topic. And my conclusion is that if I ever get a similar question on the exam and am asked to pick the most appropriate answer, I would select 'overriding' as an example of Polymorphism.
Basu Patel
Ranch Hand

Joined: May 28, 2000
Posts: 60
Thanks guys as far as I am concerned a few of us were very strong in our arguments and the correct answer is still a white mistry!....
Basu
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Overriding is clearly part of what is called polymorphism in OO. In fact, IMHO, it's a very elegant form (no pun) of polymorphism. Given the classic, albeit bastardized, example:
Mammal move() { some generic movements }
Rabbit move() { hop, hop,.. }
Whale move() { swim, swim }
Now, for example, pass a Rabbit object to moveIt(Mammal inM) { inM.move(); } to see polymorphic behavoir in action.
I'm surprised there's a debate about this...but it's a fun one.
[This message has been edited by DaB (edited August 02, 2000).]
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
In C++, method overriding is actually implemented by method
overloading. An implicit parameter "this" is what changes
overriding to overloading.
How about Java ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think I was mistaken above when I said that overloading is not a form of polymorphism. As several others have said, both overloading and overriding are aspects of polymorphism. Consider a line of code:
<code><pre> a.method(b);</pre></code>
where a and b are objects whose class may not be known at compile time, and method() is an instance method which may have been overridden and/or overloaded. The specific version of method() that gets called may depend on the class of both a and b. If you want method()'s behavior to change based on the class of a, then you must use overriding. And if you want the behavior to change based on the class of b, you must use overloading. Both are part of polymorphism.
[Note- I later realized this next paragraph is wrong, so ignore it. I only leave it here now so that subsequent comments will make sense. - Jim]
Also note that although the compiler may think it knows what classes a and b are, and generate code accordingly, it usually cannot know for sure (unless the types of a and b are final, or b is a primitive). Overriding is always resolved at runtime, but overloading is often ultimately resolved only at runtime as well.
Rajsim- in Java, an implicit "this" is used to resolve the method to be used in case of overriding. I don't know about the rest of your statement though - it seems to me that the Java definitions of overriding and overloading break down when you try to talk about "implementing overriding in terms of overloading". I suppose that some JVM authors might think of it that way, but the details would be carefully hidden away inside the JVM implementation.

[This message has been edited by Jim Yingst (edited August 03, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
C++ uses name-mangling for overloading and a table of function pointers for overriding. C++ achieves overloading and overriding in two very different ways!
thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
Jim:
Isn't overloading ALWAYS resolved at compile time? For example, if we look at the code below:

class supr {
}
class sub extends supr {
}
public class poly {
static public void main(String[] args) {
supr o1 = new supr();
method(o1);
supr o2 = new sub();
method(o2);
}
private static void method(supr par) {
System.out.println("method with parameter type supr");
}
private static void method(sub par) {
System.out.println("method with parameter type sub");
}
}

This code will print:
method with parameter type supr
method with parameter type supr
which means that the overloading was resolved based on the type of the reference variable.
Can you explain what you mean when you say "Overriding is always resolved at runtime, but overloading is often ultimately resolved only at runtime as well."
Thanks in advance
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Thomas- you're right, I was mis-remembering and not taking the time to check my facts. Method overloading is always resolved at compile time, never runtime. Sorry about that, all.
So- some people consider only dynamically-resolved methods to be "true" polymorphism, and others also consider statically-resolved methods to be a form of polymorphism as well. So overriding is definitely polymorphism, and overloading may be polymorphism, depending on whose definition you use, I think.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Compile-time versus run-time binding isn't really a determining factor as to the "polymorphicness" of things. In C++, we also have function and class templates which, I'm pretty sure, are resolved at compile-time, and these are considered to be a feature of polymorphism.
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
Hi Thomas
Thanks for your example code.
Will it print
method with parameter type supr
method with parameter type sub
If not, can u please explain.
Secondly, in Jus Java 2, Peter van Linden clearly states that though both overloading and overriding are a form of polymorphism, oveRriding is tRue form of polymorphism.
Thanks.
thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
Thanks Jim for the clarification.
Harry, the code will print
method with parameter type supr
method with parameter type supr
because, as Jim said, overloading is resolved based on the type of the reference variable.
Well, this discussion has made me change my mind about the definition of polymorphism. As many people reiterated on this forum, I now believe that the definition of polymorphism is broader, and embraces both overriding and overloading.
Timothy Budd in "Object Oriented Programming" notes that there is very little agreement regarding terminology in the programming languages community. But he treats overloading also as a form of polymorphism. As DaB emphasized, Tim says that "the facts that the compiler can often determine the correct function at compile time (in a strongly typed language), and can generate only a single code sequence, are simply optimizations" and hence should not affect the definition.
Thanks to all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism