Which method is getting executed when they have the same implementation of the same interface?

many classes can implement the same interface
class implementing two interfaces having same method in java
class implementing two interfaces having same method in c#
two interface with same default method in java
an instance of interface can be created
how to implement two interfaces in one class in java
abstract class
how to call interface method in c#

There is an interface with a method called

foo(boolean arg1, String arg2);

Additionally I have two classes which implement this method. Now when I call the foo method from the interface, how does it decide which implementation to pick?

The 2 implementations take the same parameters and return the same object but differ in the implementation.

When you call

a.foo(b, c);

it looks at the type of the object a points to, not the type of the reference and calls the method that type implements.

Initially, it uses a virtual lookup table, which as you can image is expensive, so in the Oracle/OpenJDK JVM, it can inline up to two virtual methods dynamically based on usage so it looks more like this.

if (a instanceof Type1) {
    ((Type1) a).foo(b, c);
} else (a instanceof Type2) {
    ((Type2) a).foo(b, c);
} else {
    a.foo(b, c); // do a full vtable lookup.

C#, Now implement these interfaces in a class named as Geeks and define mymethod() method and when the user will try to call this method, it gives� The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly. C# allows the implementation of multiple interfaces with the same method name. To understand how to implement multiple interfaces with the same method name we take an example.

You cannot call methods on an interface... You need to call them on an object (an instance of a class implementing your interface). and that is the code that is called.

Two interfaces with same methods having same signature but , If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface� If only one implementation of the method is given, it is accessible to the class and all the interfaces which have the same method defined. To give the specific implementation of each interface we need to specify the name of the interface followed by dot operator. For example IFileLogger.LogError ().

I believe you asking about using interface methods in othere (default) interface methods.

Method will be chosen based on instance referenced by interface variable. Here is small example

class Scratch {
    public static void main(String[] args) {
        ClassA varClassA = new ClassA();
        ClassB varClassB = new ClassB();
        Bar interfaceVar = varClassA;
        interfaceVar = varClassB;
        varClassA.foo("call from A");
        varClassB.foo("call from B");
        interfaceVar = new Bar() { // or just interfaceVar = System.out::println
            public void printParam(String params) {
                System.out.printf("Anonimus class: %s%n", params);
        interfaceVar.foo("Anonimus call");

    public interface Bar {
        default void foo(String param) {

        void printParam(String params);

    public static class ClassA implements Bar {
        public void printParam(String params) {
                    "Called from %s wth params: %s",

    public static class ClassB implements Bar {
        public void printParam(String params) {
            System.out.printf("param from ClassB:%s%n", params);


Called from Scratch$ClassA wth params: firstCall
param from ClassB:secondCall
Called from Scratch$ClassA wth params: call from A
param from ClassB:call from B
Anonimus class: Anonimus call

Process finished with exit code 0

Java Interfaces, Java interfaces specify what methods a class implementing that interface I will get back to polymorphism later in this text. If the interfaces are not located in the same packages as the implementing class, you will also need to import is possible, but would return in a ClassCastException when executed: No, its an error. If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously. According to JLS (§8.4.2) methods with same signature is not allowed in this case. Two methods or constructors, M and N, have the same signature if they have, the same name the same type parameters (if any) (§8.4.4), and after adopting the formal parameter types of N to the type parameters of M, the same formal

Explicit Interface Implementation, When two interface members don't perform the same function, it leads to interface ILeft { int P { get;} } interface IRight { int P(); } class Middle : ILeft, If a class inherits a method implementation from an interface, that method� If 2 interfaces contains a method with same signature and same return type we have to provide only one method implementation in the implemented class. ex: interface left. {. public void m1 () } interface right. {.

Inherit Multiple Interfaces With the Same Method Name in C#, For example, we create interfaces that have methods with the same name. Now Press F5 to execute the preceding program. method of which interface is implemented in class "Test" you can use the name of the interface� The executor implementations in java.util.concurrent are designed to make full use of the more advanced ExecutorService and ScheduledExecutorService interfaces, although they also work with the base Executor interface. The ExecutorService Interface. The ExecutorService interface supplements execute with a similar, but more versatile submit method.

Overriding and Hiding Methods (The Java™ Tutorials > Learning the , The Java Tutorials have been written for JDK 8. An instance method in a subclass with the same signature (name, plus the number and the The version of the hidden static method that gets invoked depends on whether it is invoked multiple implemented interfaces that contain default methods with the same signature. Actually, it's with static interface methods that helper methods can now be declared directly in interfaces rather than in separate classes. When a superclass variable refers to a subclass object and a method is called on that object, the proper implementation is determined at execution time.

  • The one which would be "more" concrete.
  • Perhaps you could show an example and indicate the behavior you're observing?
  • thank you. I think I explained my problem poorly. In my example I have an interface which calls the method: interfaceX.foo(); There are multiple implementations of foo().
  • @M4V3N do you mean the method is overloaded? ie. there is multiple possible methods it could call for the same class?