• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method overiiding??

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have one method in superclass and exactly the same method in subclass which differs only in return type?

now is it a case of method overriding or return type also should match??

how about overloading? does method overloading takes place if method differ only by return types?
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In method overloading which method to load is decided at compile time or run time??


Polymorphism has only method overloading and overriding right?

any other features in polymorphism?
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be a case of overloading. If you wanted overiding everything would have to be the same.
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am no expert, but I am pretty sure that the JVM looks for a method in the class that is calling it. If it can't find it, it looks in the superclass and so on and so forth until it reaches object. An overriden method would be reached before the superclasses method. An overloaded method is a method with the same declaration but different parameters. So the JVM is looking for the one with the parameters you specify?

When you say other features of polymorphism what do you mean? One might be a subclass can be used any time a superclass is needed.
[ November 22, 2007: Message edited by: colton peterson ]
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I thought you said parameters. With return types I am not sure but I am guessing it overriding because the JVM is looking for a method with the same parameters.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by colton peterson:
That would be a case of overloading. If you wanted overiding everything would have to be the same.

Well not anymore. Java 5.0 introduced a new feature (can't think of the name*), which allows you to downcast the return type - you can return any type as long as it is a subclass of the return type of the original method.

This can be very useful for cloning:

The reason this is allowed is because any calling class expect something of the original method's type (Object in the example). Any subclass will respect that expectation.

* Just remembered: it's called covariant return.
[ November 22, 2007: Message edited by: Rob Prime ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic