• Post Reply Bookmark Topic Watch Topic
  • New Topic

polymorphisam with overloading  RSS feed

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even though most of the people and google and its web sites say the method over loading is also a kind of polymorphisam. why does SCJP book written by bret bates and kathy seyrra say polymorphisam is for method overridding not for method overloading.

please give me some explination in this regard.

thanks in advance

rahul.
 
Matteo Di Furia
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, when you override a method you're basically defining a specialized version of that method for your class. When a program calls a method on an object, Java executes the most specialized version of that method in the class diagram, starting from the declared type of the object down to the extending classes till the exact type of the runtime object instance on which you call the method.
When you overload a method, instead, is like adding a brand new method to this class. If the overloaded method is not overridden in child classes, a program calling a method will execute that exact implementation of the method.
This said, imagine to define two classes, A and B. Class A define a method, for example doSomething(int a). Class B override this method and overload it with doSomething(String c). Suppose you got a program that instanciate classes like this :



now, you can call :



and, what's most important in what I'm trying to explanate, you'll get the B's implementation run. Thus A acts like B, it's somehow ... polymorphic. On the other side, instead, you can't call



since it is not defined for class A.
This is why, as I intended OO programming, overriding is more related to polymorphism than overloading is (I'd say overloading is not concerned at all with polymorphism).
[ July 30, 2008: Message edited by: Matteo Di Furia ]
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first hit I get on Google is the Wikipedia page for method overloading, and it states:
Method overloading should not be confused with ad-hoc polymorphism

Polymorphism being defined by Wikipedia as:
a programming language feature that allows values of different data types to be handled using a uniform interface.

That definition of polymorphism does not exclude using method overloading, but it does not say that method overloading alone is a kind of polymorphism.
Maybe you can cite a source?
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
The first hit I get on Google is the Wikipedia page for method overloading, and it states:

That definition of polymorphism does not exclude using method overloading, but it does not say that method overloading alone is a kind of polymorphism.
Maybe you can cite a source?



http://home.cogeco.ca/~ve3ll/jatutor5.htm
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rauhl Roy:

http://home.cogeco.ca/~ve3ll/jatutor5.htm


That guy appears to be a math teacher writing a simplified introduction to the Java language. I wouldn't take his one-page introduction as a canonical source.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:


That guy appears to be a math teacher writing a simplified introduction to the Java language. I wouldn't take his one-page introduction as a canonical source.
Least of all when he tells us about the java.lang.Object#copy(java.lang.Object) method. :roll:
[ July 31, 2008: Message edited by: Campbell Ritchie ]
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matteo Di Furia
specialized version of that method


What does specialized version of that method mean ?
You mean "the most specific Class's method"?
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Matteo Di Furia

you'll get the B's implementation run


No,

if



it will run the A's method implementation. Not B's.
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Shilpakar:

if

A obj = new B();
obj.doSomething(2);[/CODE]

it will run the A's method implementation. Not B's.


Something that tends to happens on these forums is that people
arguing about topics (such as polymorphism) can confuse the
readers (especially greenhorns) if they aren't careful.

So I propose that we do be careful. I suggest that we who post here
give runnable code examples and actually try them out before posting.

----

Now, as to whether obj.doSomething() invokes A's or B's implementation,
that depends on whether doSomething() is static or not.

The normal assumption when writing obj.doSomething() is that doSomething()
is not static, because if it were static the normal way to call it would
be either A.doSomething() or B.doSomething(), not obj.doSomething().

So here's a sample non-static case:

A -> java.lang.Object
B -> java.util.Date
doSomething -> toString

Object obj = new java.util.Date();
String result = obj.toString();


Would you expect result to be something like
"java.lang.Object@123456" (A = Object's implementation)
or "Fri Aug 01 12:13:14 EDT 2008" (B = Date's implementation)?

If you actually run it you'll see it is B's implementation. That should
not be surprising, since non-static methods are polymorphic in Java.

So unless Rahul Shilpakar was considering the case where doSomething()
is static, I must disagree with him.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mr. Brain,

So, your conclusion is that



if doSomething() is static it takes the implimention from the A;
And if it is non-static it will take the implemention of B()..?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the original question: people mean different things by "polymorphism". Many people, when talking about polymorphism, actually mean runtime polymorphism (I guess that's the same as the ad-hoc polymorphism mentioned above). Method overloading would *not* be an example of this tight definition.

In a more general sense, polymorphism can also include things like "compile time" polymorphism - which would include method overloading, and other mechanisms, such as C++ templates.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i know a static method can not override the an instance method. so may i know how the static come into picture here. may i know?
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rauhl Roy:
As far as i know a static method can not override the an instance method. so may i know how the static come into picture here. may i know?


It was in the context of this code:

A obj = new B();
obj.doSomething();


If the doSomething() method is static, then A's implementation
of doSomething() will be run, even if B also has an implementation.
(You are correct that static methods can not override.)

If doSomething() is not static [which is typically the case when it
is written obj.doSomething()] then B's implementation will be
run (presuming B has overridden A's implementation).

That's it.
[ August 04, 2008: Message edited by: Brian Cole ]
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then why do not we directly use the B obj = new B(); ?

do not you think this is called runtime pholymorpisam.

because myeclipse show



obj.doSomething()= packagename.A.doSomething();

but when you run it. it works properly i.e taking the B's implementaion.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!