File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java 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 » Beginning Java
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Sudipta Das
Greenhorn

Joined: Apr 05, 2001
Posts: 18
Which is an example of Polymorphism ?
A. Method Overloading
B. Method OverRiding
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Overloading = multiple methods in SAME class - different signatures. Class uses whichever matches the way it was called.
<pre>
class Super{
public void coolMethod(){ //this method takes NO parameters
}
public void coolMethod(String s){ //this overloaded method takes a string parameter
// do cool super stuff
}
}
</pre>
overriding = same method and signature in child class. Child class uses the overridden method unless instructed specifically to use parent class by using "super." syntax.
<pre>
class Child extends Super{
public void coolMethod(String s){ //this method is overriding the matching method in the parent
// do cool Child stuff
}
}
</pre>

[This message has been edited by Cindy Glass (edited April 05, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
So the answer is B


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Read THAT question wrong. Somehow I read it as give me an example of . . .
tony hutcheson
Ranch Hand

Joined: Dec 08, 2000
Posts: 38
From what I understand, neither is an example of Polymorphism. However, method overriding is a part of it.
From "Beginning Java 2" by Ivor Horton: "Polymorphism...means the ability of a single variable of a given type to be used to reference objects of different types, and to automatically call the method that is specific to the type of object the variable references." (p231 JDK1.3 edition)
example (also from the above mentioned book):
public class Animal{
private String type;
public Animal(String aType,){
type = new String(aType);
}
public String toString() {
return "This is a " + type;
}
}
public class Dog extends Animal{
private String name;
public Dog(String name){
super("Dog");
name = new String(aName);
}
public String toString() {
return super.toString() + " It's name is " + name;
}
}
public class Cat extends Animal{
private String name;
public Cat(String name){
super("Cat");
name = new String(aName);
}
public String toString() {
return super.toString() + " It's name is " + name;
}
}
You can then do the following:
Animal[] theAnimals = { new Dog("Rover"), new Cat("Max")};
System.out.println(theAnimals[0]);
System.out.println(theAnimals[1]);
and you would get -
This is a dog It's name is Rover
This is a cat It's name is Max
That is polymorphism - where the Animal class can refer to the objects of the Dog and Cat class and use the appropriate methods.
Hope this helps.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
But you can't do polymorphism without method overriding.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

what about interfaces?

if two different classes implement the same interface, can they be said to be overriding the method "actionPerformed" or are they providing a 'first' implementation.

overriding implies there is code to be accepted and extended, or rejected and 'overriden'. But with an interface, you must provide an implementation, and each class implementing the interface must re-provide it.

you can get polymorphic behaviour in this fashion, and it doesn't technically require overriding.

Am I right? I've been beating my head against the wall with J2EE stuff these past two days, so I'd not be surprised at all if I was off my pumpkin.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Technically it is overriding. We override abstract methods in order to implement them.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Read here for more information, sample code, etc. etc.
Methods Overloading and Overriding
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
In reference to the quoate that Tony posted earlier:
From "Beginning Java 2" by Ivor Horton: "Polymorphism...means the ability of a single variable of a given type to be used to reference objects of different types, and to automatically call the method that is specific to the type of object the variable references." (p231 JDK1.3 edition).
I think you can use either overridding or overloading for polymorphism.
Overloading:
Class Hammer and class Saw inherit from class Tool. Class HandyMan has a method for cleaning all his tools
public void cleanTools(Tool[] tools) {
for (int i=0; i<tools.length; ++i)
this.clean(tools[i]);
}
Class Handyman has an overloaded method for each of his tools.
public void clean(Hammer hammer) {
// clean hammer
}
public void clean(Saw saw) {
// clean saw
}
Overriding:
Tool classes are the same as above except that Saw and Hammer override a clean method in Tool. The Handyman calls the tool class method to clean itself.
public void cleanTools(Tool[] tools) {
for (int i=0; i<tools.length; ++i)
tools[i].clean();
}
[This message has been edited by Mark Savory (edited April 06, 2001).]
[This message has been edited by Mark Savory (edited April 06, 2001).]
[This message has been edited by Mark Savory (edited April 06, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Not to disagree with Ivor (who has written a great book) but "Design Patterns" says that polymorphism is "the ability to substitute objects of matching interface for one another at runtime" (my emphasis). Method overloading occurs at compile time so it would not qualify as polymorphism.
[This message has been edited by Thomas Paul (edited April 06, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Your example also has a problem... why would class Hammer have a mthod to clean an object of type Saw?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Having just looked at Ivor's description, I think you misread what he wrote. The diagram makes it quite clear what he is trying to explain. He shows a class Dog with 3 classes extending it, Spaniel, Chihuahua, and Collie. He then shows that if you run aDog.bark() that the proper Dog object will bark depending upon which one was instantiated.
He then lists the preconditions for polymorphism:
1) the method call for a derived class must be through a variable of a base class type
2) the method called must also be a member of the base class
3) the method signature must be the same in the base and derived classes
4) the method return type must be the same in the base and derived classes
5) the method access specifier must be no more restrictive in the derived class than in the base class
Your example fails in 1, 2 and 3.
[This message has been edited by Thomas Paul (edited April 06, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
"Polymorphism...means the ability of a single variable (aDog) of a given type (Dog) to be used to reference objects of different types (Collie, Spaniel, Chihuahua), and to automatically call the method (aCollie.bark(), aSpaniel.bark(), aChihuahua.bark())that is specific to the type of object(Collie, Spaniel, Chihuahua) the variable references."
I think Ivor is quite correct and you have misunderstood his meaning.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Peter van der Linden says, "Polymorphism is ... Greek for "many shapes," and it merely means using the same one name to refer to different methods. "Name reuse" would be a better term. There are two types of polymorphism in Java: the really easy kind (overloading) and the interesting kind (overriding). ... [a description of overloading follows] ... The second, more complicated kind of polymorphism, true polymorphism, is resolved dynamically at runtime. [a description of overriding follows]."

So it appears that Mark was not entirely wrong. However, usually when polymorphism is discussed, the kind using overriding (true polymorphism) is the type being discussed.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I think Peter is mistaken. Overloading is not an example of polymorphism. Certainly it isn't in any of the OO design books that I have read.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism
 
Similar Threads
Dynamic and Static Polymorphism
static members and inheritance
Method Overloading
Dynamic polymorphism and Overriding
Variable Polymorphism