• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method overiiding??

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic