File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Inherited methods in Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inherited methods in Interfaces" Watch "Inherited methods in Interfaces" New topic

Inherited methods in Interfaces


Joined: Apr 11, 2005
Posts: 2
If an interface inherits two methods without overriding and differ only in their exceptions then how to implement them in a class that implements the subtype interface. Plz illustrate with an example.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!

Now, to answer your question: if you can't write a single method that satisfies both exception specifications, then you simply can't implement both interfaces in one class, end of story. This issue is specifically described in Gosling and Arnold's "The Java Programming Language."

[Jess in Action][AskingGoodQuestions]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Two methods cannot be declared with only difference being the exception declaration, as this amounts to a duplication of methods which is disallowed.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Rex Farmer

Joined: May 05, 2005
Posts: 1
I think he's talking about two interfaces with identical methods on them that differ only by their exceptions. That's perfectly legal, but their implementation must be the same method, and the exception signature of any implementation must be compatible with both superinterfaces.

In general, it's always OK to take exceptions out of method signatures for subtype relationships, but you can't add any in.

You wanted examples:

Note that in this example, it's not OK for to declare either ParseException or IOException.
I agree. Here's the link:
subject: Inherited methods in Interfaces
It's not a secret anymore!