• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Polymorphism

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer "Overloading" is correct.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Jim. I have always associated polymorphism with dynamic method look-up that occurs with overRIDING methods.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic