Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
Sudipta Das
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is an example of Polymorphism ?
A. Method Overloading
B. Method OverRiding
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the answer is B
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read THAT question wrong. Somehow I read it as give me an example of . . .
 
tony hutcheson
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can't do polymorphism without method overriding.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically it is overriding. We override abstract methods in order to implement them.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read here for more information, sample code, etc. etc.
Methods Overloading and Overriding
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic