Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance

 
rafa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rafa
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
These compile just fine:

And so do these:
 
Alex Ku
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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?
Thanks
Kawaii
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rafa,
it should work. i don c any problem with it. u can post ur code if its not big.
regards
maulin.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maciej Kolodziej
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that's what I wrote about 6 posts ago
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic