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 inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "inheritance" Watch "inheritance" New topic



Joined: Feb 13, 2002
Posts: 6
I implement an interface and extends a Class
class and interface both got a common method
So I get an error
how to solve this
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!

What error are you getting in your code? The interface has no method definition for the implementing class to inherit - in fact the implementation it inherits from the class it is extending should satisfy the interface.

Maciej Kolodziej

Joined: Feb 11, 2002
Posts: 26
Are these methods exactly the same?
You can to what You want only if methods are exactly the same (same arguments and return types) or if they have different argument types or arguments count.
[ February 13, 2002: Message edited by: Maciej Kolodziej ]

Frank Lin
Ranch Hand

Joined: Jan 26, 2001
Posts: 76
Since Java does not support multiple inheritance, I don't think you can extend a class and implement a interface which have a common method with same signature.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
These compile just fine:

And so do these:

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
I guess since interface has no implementation. At run time, when you can super, there is only one implemented method, which is the class you extends. So, there is no confusion. Am I right?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi rafa,
it should work. i don c any problem with it. u can post ur code if its not big.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
It occurs to me that you might have a class and an interface which have a method with the same name and signature, but different return types. In this case it would [i]not[/] be possible to extend the class and implement the interface in the same class. There might be similar problems if the access modifier or throws clauses are different, but these can be circumvented as long as the new method (a) is declared public, and (b) does not itself declare any exceptions thrown which are not declared in both other versions of the method. You can always override a method with one which is more public and throws fewer exceptions - but you can't change the return type.

"I'm not back." - Bill Harding, Twister
Frank Lin
Ranch Hand

Joined: Jan 26, 2001
Posts: 76
I was thinking what would happen if super.method() is called. It proves to be fine since interface does not have a implementation for the method. But it is really confusing to me when it comes to this kind of inheritance hierarchy.
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Method implementations only happen in classes. All an interface does is define a set of methods that classes can declare they implement. If class A implements interface I, that just means class A needs to implement the methods defined in I.
If A extends another class, that superclass doesn't have to implement I, so it's quite possible methods in I won't be present in the superclass. In this case, calling super.method() has the same exact effect as when you try to call a method that doesn't exist, either in the superclass or just in a regular class.

SCJP 1.4
Maciej Kolodziej

Joined: Feb 11, 2002
Posts: 26
And that's what I wrote about 6 posts ago
I agree. Here's the link:
subject: inheritance
It's not a secret anymore!