Reputation: 687
interface Block{
void printblock();
}
interface Block2{
void printblock();
}
class Impl2 implements Block,Block2{
@Override
public void printblock() {
// TODO Auto-generated method stub
System.out.println("Impl2");
}
}
public class Import
{
public static void main(String[] args) {
new Impl2().printblock();
}
}
Now please say me which printblock method is implementing by the class Import.Its implementing Block interface or Block2 interface?
Upvotes: 2
Views: 984
Reputation: 6054
Syntactically, an interface doesn't really mean anything other than the fact that some method of a certain name exists.
Semantically, however, the power of interfaces is that they form a "guarantee" of an implementation of some method to be in accordance with the behavior described by the interface itself. That is, by choosing to implement Block or Block2, you are contracting yourself to their behavioral expectations (specifically, containing their method).
In your example, the fact that your class implements both Block and Block2 is an explicit statement that you are implementing both of them--don't be confused by the fact that their method names are the same! The syntax only tells you that the method EXISTS in Impl2. What's more important is what behaviors Block and Block2 define/expect.
For example, if you had two interfaces called List and Set, and wanted one data structure to be able to represent both semantically, you may have a size() method that is the same for both, and thus it implements both List.size() and Set.size().
Of course, interfaces are also very easy to abuse if you are not careful with semantics. You would have big issues implementing both List and Set if they defined the same add() method, because by definition, the List.add() behavior allows duplicates, and Set.add() does not. So there is a trap here--you may think you can implement both interfaces based on similarities in some methods, but it turns out they define fundamentally different behaviors in other methods.
In general, it seems to me like any time you are implementing two interfaces at the same time with the same method, something is wrong with the class design. Perhaps what you really wanted to do was combine shared behavior into a superinterface, and implement that. It would be a much stronger construct since you would be explicitly defining the shared portions of the interfaces, and wouldn't run into implementation issues with the distinct portions.
Upvotes: 0
Reputation: 577
When you are writing
new Impl2().printblock();
It doesnt matter what Interface the method printblock is part of.
If you write something like
Block block1 = new Impl2(); block1.printlblock(); or similarly block2.printblock().
Here now both are valid statements and since both have the same method definition, same method will be executed. Interesting thing is because their definition is same its not possible to implement same method definitions in two different ways(though you want it differently for each of them)
Your question has already been answered here
Upvotes: 1
Reputation: 108937
Interface is just a contract and in your case there are two different interfaces vouching that that a class has a method implemented with a particular signature so it doesn't matter which interface the method belongs to.
Upvotes: 3
Reputation: 59650
As both interface have same method signature both method will be implemented by Impl2
class as a one method. Impl2 class is implementing a method printblock()
with the same signature both interfaces has.So you can say that Imlp2 is implementing method of both the interfaces.
Upvotes: 2