• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 24204
44
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.
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic