How to find out which interface a method was called on?

java

Solution

Neither. The idea of an `interface` is that a class that implements it, agrees with the "contract" the interface implies. If you have two interfaces, requiring both to implement a method `work()`, and a class that implements both interfaces, then it has to implement `work()` to agree with the contract of both.

The JavaDoc says:

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

And that is exactly what you do by implementing a `work()` method: you satisfy both interfaces `A` and `B`.

Problem

I have a simple Java question. Consider the following interfaces: ``` interface A { void work(); void a(); } interface B { void work(); void b(); } ``` So when a class is going to implement them, it should be like this: ``` class Impl implements A, B { void work() { /*some business*/ } void a() {} void b() {} } ``` My question is, in `work` method, how would I find out that, it has invoked by type `A` or `B`? The above class in C# would be like this, and this separates both implementations very well: ``` class Impl : A, B { void B::work() {} void A::work() {} void a() {} void b() {} } ``` But how would I achieve something like C# model in Java?! Thanks in advance.

Original source