• 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

Overriding Method Arguments

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wish to confirm. When override method in subclass, the rule is that argument list must be exactly the same. My question is, does this mean arguments must have exactly same names as well as type?

For example is below okay for overriding?
Parent method
public int getResult(int theNumber){}

Sublass method
public in getResult(int anotherNumber){}

I have tried this and compiled and ran okay. I think it is overrided rather than overloaded as haven't changed enough (number or type of arguments).
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, as you've observed, the names don't matter -- only the type and number of the arguments. The return type must be the same too, although if that's the only thing that's different, the code won't compile.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
... The return type must be the same too, although if that's the only thing that's different, the code won't compile.


I'm sure you meant to say, "The return type can differ too..."
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm sure you meant to say, "The return type can differ too..."


The return type of an overriding instance method or a hiding static method must be identical to the superclass method or you will get a compiler error.

I think that Ernest's point is that differing formal parameter types result in method overloading instead of overriding, while differing return types result in an error.
[ March 04, 2005: Message edited by: Mike Gershman ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops! I made the fatal error of confusing overridding and overloading.

Sorry Ernest.
 
reply
    Bookmark Topic Watch Topic
  • New Topic