• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Interfaces

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at the below code. Could you let me know which interface method "test" is implemented

Edited by Corey McGlone: Added CODE Tags
[ March 11, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well... both.
The interface just says: the implementing class will have this method. It doesn't provide the implementation, so you shouldn't ask "which method is implemented" just whether it's implemented or not
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It imlements both methods. From the JLS, §8.1.4 Superinterfaces:
"It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:

the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano; it is considered to implement both."

You can see that they are both implemented in your example if you modify the code slightly:

I hope that helps,
Corey
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is both, since it satisfies the requirement for both the interfaces and both are happy in this case. However if we do something like this

What will happen in case like this?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Jay Patel:
"What will happen in case like this?"

In a case like that, you're in trouble. Unless I'm mistaken, you can't make that compile unless the implementing method throws neither exception. You can write it like this and it will compile:

This works because, when you override a method, you are allowed to throw any exceptions that are defined in the overridden method or any subset of those exceptions. In this case, we're throwing no exceptions, which is the empty subset of the exceptions defined in the overridden method.
However, the minute you try to throw an exception, you're in trouble. Let's say we want to throw SomeException1:

This won't compile because we're trying to override i1.test() which can throw SomeException1 and i2.test() which can throw SomeException2. Well, this works fine for overriding i1.test() because that method can throw SomeException1. However, we get a compiler error on the overriding of i2.test() because that method can't throw SomeException1 - this breaks a rule of method overriding.
If you flip this around, or try to say that Test.test() can throw both exceptions, you'll get similar errors.
If you try to define two versions of Test.test(), one that can throw SomeException1 and another that can throw SomeException2, you'll run into another error - you now have two methods defined that have the same signature because the exceptions that can be thrown are not considered part of the method signature. Of course, having two methods with the same signature is a no-no.
So, what's the solution? Well, you're going to have to rethink your design in this case. Perhaps a simple rename of i2.test() to i2.test2() or something along those lines. As it stands right now, I don't see a way to make that compile.
I hope that helps,
Corey
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Corey, I found explaination along same lines in JLS too, here is what it says, its not exactly related to throwing different exceptions but it explains reason why we cant do it.

____________________________________________________________
On the other hand, in a situation such as this:
interface Fish { int getNumberOfScales(); }
interface StringBass { double getNumberOfScales(); }
class Bass implements Fish, StringBass {
// This declaration cannot be correct, no matter what type is used.
public ??? getNumberOfScales() { return 91; }
}
It is impossible to declare a method named getNumberOfScales with the same signature and return type as those of both the methods declared in interface Fish and in interface StringBass, because a class can have only one method with a given signature (�8.4). Therefore, it is impossible for a single class to implement both interface Fish and interface StringBass (�8.4.6).
_______________________________________________________________
reply
    Bookmark Topic Watch Topic
  • New Topic