-1

I wrote a program in Java with 2 interface and a class.

Both the interface has the same method name.

In the main class i am implementing both the interfaces and called the method. I want to know which interface method is called...

Here is the sample code :-

public interface A {
    void print();
}

public interface B {        
    void print();
}

public class C implements A, B {

    public static void main(String[] args) {
            C c = new C();
            c.print();
    }

    public void print() {
        System.out.println("sample");
    }   
}
The Dr.
  • 556
  • 1
  • 11
  • 24
  • 5
    "*I want to know which interface method is called*" => neither: C's method is called... – assylias Sep 20 '15 at 10:40
  • 3
    possible duplicate of [Implementing two interfaces in a class with same method. Which interface method is overridden?](http://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method) – smoggers Sep 20 '15 at 10:40
  • 1
    Actually you don't call a interface method. You implement it and call the method of the class that implement that method. – accurate respondetur Sep 20 '15 at 10:41

4 Answers4

2
public interface A {
    void print();
}

public interface B {
    void print();
}

In the above code the interfaces A and B are abstract interfaces, because some/all methods are declared but not defined.

Hence in your C class you are not calling any of these two (which is straightforward, how would you be able to call a method which was never defined?). What you are doing is defining the print method (hence giving it a body), to call it afterwards (in main).

Kevin
  • 2,813
  • 3
  • 20
  • 30
  • @RavishankarChoudhary Please consider accepting an answer. – Kevin Sep 22 '15 at 10:57
  • Hi there Although i have declare interface and Technology not allowing us to define methods inside interfaces rather it can declare,so two different interfaces may have their own declarative methods with the same name. – Ravishankar Choudhary Jun 14 '16 at 11:44
0

You call the class's method

 public void print() {
    // TODO Auto-generated method stub
    System.out.println("sample");
} 
Andrei Amarfii
  • 685
  • 1
  • 6
  • 17
0

It's C's print(). abstract methods don't have implementations and are not called per se, it's one of their implementations that's called.

You may want to look into what's called static binding and dynamic binding. In this case to the compiler & runtime everything is at compile time, so the former is employed. Basically it will be statically determined that the method / implementation you want to call is C's print().

Dynamic binding would still mean calling a concrete implementation of a method, so it wouldn't choose an interface method per se, but the choice of which method to call will be done at runtime.

async
  • 1,537
  • 11
  • 28
0

Interface which is implemented first in a way that interface's method will call and if another interface is next to the first one have same method name that will not going to call at all.