• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with deadly diamond of death  RSS feed

 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if have read that interfaces can extend any number of classes! isnt it going to something same as deadly diamond of dead. i mean what if some one extended an inteface with two classes which have two same methods. which method will get implemented?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interfaces can only extend other interfaces, not classes. Since interfaces can't implement any methods, there are no questions about where the implementation of a method comes from -- it can only come from a class that eventually implements the interface.
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class implements 2 interfaces, both having similar method declarations , then the class should implement the method only once.

The compiler would not complain. Both the interfaces are satisfied because their method is implemented in class.

Hope this clarifies
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well what if i created my own interface lets say A and i define some methods in it, now again i create an interface B that extend two interfaces lets say A and C (i haven't created that one).now by chance there is a method which have the same name , return type and the argument in both the interfaces A and C, but they are doing two different thing in the method implementation. now i made a class which implements interface B and i called that same method. now which method will get called?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's only one method -- the one you defined in the class. It will get called.

Don't try to ask any more complicated questions -- that's all there is to it!
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i got it.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, although syntactically your code will be correct, you violate the contract of one of the interfaces (semantically). It's therefore a sign of bad design, and you'll need to refactor your code.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dude. Interface is a contract.

It does not matter if Interface A and B has the same method signature...

So if

the class, has to follow the contract "public void methodZZ()" and will pass on the test IS-A "B and A".
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks hebert for your reply but i have figured that out already and my problem is already resolved.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!