3

I have declared an interface

public interface A {
    public void print();
}

another interface

public interface B extends A {
    public void print();
}

Then class

public class C implements A,B { 
    @Override
    public void print() {
        System.out.println(B.i);
    }
}

Kindly tell me why there is no compile time error when both the interfaces have methods with same name and how does compiler determine which interface's method is being implemented??

thanks in advance

rishi
  • 1,792
  • 5
  • 31
  • 63
  • 1
    An interface is a contract that your implementing class has to adhere to. Since neither contract is broken, it is valid code. – Jeroen Vannevel May 25 '14 at 13:19
  • Read it here [Interface implementation hiding method](http://stackoverflow.com/questions/11932037/interface-implementation-hiding-method) – Braj May 25 '14 at 13:21

3 Answers3

3

Implementing an interface in Java means, guaranteeing that methods, that are declared in the interface are implemented into the class.

In your example this means, that every class that implements interface A has a print-function, that takes no arguments and returns a void. Same goes for interface B. Every class that implements B has a print-funtction that take s no argument and returns a void.

If now class C implements both interfaces A and B, this means, that your class has to implement all methods that were declared in A and all methods that were declared in B. In your example these methods overlap, but do not conflict. Therefore there is no problem in that.

LorToso
  • 286
  • 1
  • 9
2

Your method implements both interface methods at once.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Interface B override interface A method..

When you use Interface you say to Java: "I will implement this thing" Once the B override A you say to Java: "I will implement the same thing also on B";

So if you are implementing this once.. Basically it is like you implemented The method for both of them.. And this is not the right way to use this

Aviad
  • 1,539
  • 1
  • 9
  • 24