• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inherited methods in Interfaces

 
ravi
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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."
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two methods cannot be declared with only difference being the exception declaration, as this amounts to a duplication of methods which is disallowed.
 
Rex Farmer
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 D.xyz to declare either ParseException or IOException.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic