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

Overriding Method Arguments

Craig Oliver
Greenhorn

Joined: Aug 25, 2003
Posts: 21
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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..."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Whoops! I made the fatal error of confusing overridding and overloading.

Sorry Ernest.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding Method Arguments
 
Similar Threads
Overriding Vs. overloading
interface problem
for overloading..?
Inheritance and Polymorphism
Static method Overriding- confusion