aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overload methods in subclass? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overload methods in subclass?" Watch "Overload methods in subclass?" New topic
Author

Overload methods in subclass?

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overload methods in subclass?
 
Similar Threads
Override class IOException
Reg. Overriding : Valiveru's exam
method overloading and overriding from subclass
overriding
Overidding Methods