I was asked this is an interview, and have not found an answer.
-
2With 'same prototype'? What's that's supposed to mean? – Erik Pragt Jul 10 '13 at 19:01
-
@ErikPragt I edited for clarity: prototype == method header (C-terminology). – Steve P. Jul 10 '13 at 19:15
4 Answers
Is that possible?
Yes it's possible. That won't make any difference. Your class still have to provide a single method definition. And that will satisfy contract of both the interfaces.

- 209,639
- 45
- 409
- 525
-
-
Please see the question linked as duplicate. That contains an example. – Rohit Jain Jul 10 '13 at 19:27
It is acceptable, though if the two interfaces provide for a different contract or different expected behavior, your code may encounter issues from subtle, hard-to-debug issues as you could pass the object to a method that expects the intricacies of one interface but encounters intricacies of the other.

- 40,330
- 4
- 86
- 117
-
+1 for addressing expected behavior that may not be enforced by the compiler – Code-Apprentice Jul 10 '13 at 19:13
Yes, it's possible. If both prototypes, or method headers, are the same, you simply write the method in your class, and you've inherently satisfied the contract with both interfaces (assuming all other methods are also included).
The interface does not tell you how to implement something, it is a contract that mandates that certain methods appear in classes that implement the interface. Now, there may be an issue with "expected" conflicting behavior; but, if by design, you're supposed to implement both interfaces, this is a perfectly acceptable practice.
EDIT: Here's a (requested) example:
public interface One
{
void test(String x);
}
public interface Two
{
void test(String x);
}
public class InterfaceTest implements One, Two
{
public void test(String s)
{
System.out.println(s);
}
}

- 14,489
- 8
- 42
- 72
-
ok can you provide me a rough code example e.g. public interfacae A.... with just a prototypes and its calling ... 2 interfaces are required and on class from where that functions called – Arslan Tariq Jul 10 '13 at 19:20
-
-
For additional information, see [here](http://stackoverflow.com/questions/9863835/two-interfaces-with-same-method-signature-implemented-in-java-class). – Steve P. Jul 10 '13 at 20:10
-
No problem, since you're new, you should check out the [tour](http:stackoverflow.com/about). It talks all about what good questions/answers are, upvoting/downvoting and accepting answers, etc. Good luck. – Steve P. Jul 10 '13 at 21:40
Yes it is possible. There are some interfaces that have no required methods at all. Serializable is one.

- 11,265
- 16
- 66
- 92
-
The OP is referring to the situation where two interfaces specify the same method. Methodless interfaces don't truly fall under the scope of the question. – nanofarad Jul 10 '13 at 19:10
-
-
I was referring to the other extreme of two interfaces in one class. The two interfaces can refer to the same methods or have nothing in common. – Ayman Jul 10 '13 at 19:24
-
@ArslanTariq Just look at the GNU classpath source code to clue you in. This is more of a concept than a code example. – nanofarad Jul 10 '13 at 19:27