Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overload methods in subclass?

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was always under the impression the overloaded methods existed in the same class. I just need some validation and elaboration on my code comments, thanks.

A: illegal override since the overriding method is less restrictive
B: legal override which can throw fewer exceptions the overriden method; accessibility, return type, and types of arguments MUST be identical.
C: illegal override since overriding method may not throw more strict exceptions than parent
D: an overloading method here since its the same method name as parent, however non identical argument types (Is this correct?) Since it is an overloaded method (alledgedly) there is no constraint on accessibility, return type or exceptions which may be listed in signature
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I have a few comments to make here:

A: illegal override since the overriding method is less restrictive

Actually, it's not less restrictive, it's more restrictive. Default accessibility is more restrictive than public accessibility.

B: legal override which can throw fewer exceptions the overriden method; accessibility, return type, and types of arguments MUST be identical.

I don't think it's quite correct to say that an overriding method can throw "fewer" exceptions. It can throw a subset of the exceptions defined by the overridden method, but it can also throw subclass of any exceptions in that subset. For example, if the method overridden throws Exception, the overriding method can declare that it throws IOException and MalformedURLException, which are both subclasses of Exception.

C: illegal override since overriding method may not throw more strict exceptions than parent

See my response for B.

D: an overloading method here since its the same method name as parent, however non identical argument types (Is this correct?) Since it is an overloaded method (alledgedly) there is no constraint on accessibility, return type or exceptions which may be listed in signature

Well, you have to remember that, when a method is inherited, it is part of the subclass. Therefore, in this case, since class B extends class Test1, class B has a method called aMethod that takes two floats and throws an IOException. Now, in class B, you are free to define any other methods that you want that have the name aMethod and these must abide to the rules of overloaded methods. However, if you have another method with the same name and signature of aMethod, this is considered an overriding method. So, in short, you can actually overload an overridden method (although, I don't think I'd ever actually say it that way ).
I hope this helps clear things up for you,
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic