Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding Method Arguments

 
Craig Oliver
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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..."
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops! I made the fatal error of confusing overridding and overloading.

Sorry Ernest.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic