aspose file tools*
The moose likes Beginning Java and the fly likes method overiiding?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method overiiding??" Watch "method overiiding??" New topic
Author

method overiiding??

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
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

Joined: Apr 04, 2007
Posts: 188
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

Joined: Nov 18, 2007
Posts: 97

That would be a case of overloading. If you wanted overiding everything would have to be the same.


www.mormon.org
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

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

Joined: Nov 18, 2007
Posts: 97

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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: method overiiding??