0
public  class Test implements X, Y { //X.Y interface are shown below        
       public void myMethod() {
           System.out.println(" Multiple inheritance example using interfaces");
       }

     public static void main(String[]args) {             
         Test t=new Test();
         t.myMethod();
         System.out.println(t.a);  //compile time error ambigious field     
     }      
}

Please help me to solve this issue

 interface X {
       public void myMethod();
       int a = 0;
 }

 interface Y {
       int a = 9;
       public void myMethod();
 }
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
mohd aarif
  • 11
  • 5

3 Answers3

1

Any variable defined in an interface is, by definition, public static final, in other words it's just a constant, it's not really a field (since there are no fields in interfaces).

So the compilation error you get points out that the compiler doesn't know witch constant you refer to.

You have 2 options here:

  1. change the name of the constant in one of the interfaces, for example in interface Y declare int b = 9;

  2. inside the main method point to a concrete constant: System.out.println(X.a);

theDima
  • 746
  • 4
  • 12
0

Adding to one of the answer already provided.

If a class implements two interfaces and each interface have method with same signature and same name, then you are effectively defining only one method and they are same. If you have two methods of same name but different return types then there will be a compilation error.

Example ->

public interface A {
    int a = 0;
    void myMethod();
}

public interface B {
    int a = 0;
    void myMethod();
}

public class Test implements A, B {
    @Override
    public void myMethod() {
        // My method is defined for both A and B
        System.out.println(" Multiple inheritance example using interfaces");
    }

    public static void main(String[] args) {
        Test object = new Test();
        ((A)(object)).myMethod(); 
        ((B)(object)).myMethod();
        System.out.println(((A)object).a); //To print constant of A
        System.out.println(((B)object).a); //To print constant of B
    }
}

//Let's see other example

public interface A {
    void myMethod();
}

public interface B {
    boolean myMethod(); //changed void to boolean
}

public class Test implements A, B {
    @Override
    public void myMethod() { //Compilation error here, return type is incompatible
    }
}
Sneh
  • 3,527
  • 2
  • 19
  • 37
  • You can refer to the `a` and `b` fields directly - they are implicitly `public static final` - `A.a` and `B.a` work as well. – Clashsoft Jan 16 '16 at 16:56
  • @Clashsoft yeah I know that but as In tje original question the asker was trying to use object therefore I showed with the help of an object. – Sneh Jan 16 '16 at 17:03
0

myMethod implements both myMethod declarations. I believe your problem is that in the two separate interfaces a has different values and is before and after the declaration of myMethod. The order is not important, since myMethod will surely be called after the declaration of a, but the difference of a values might cause some logical discrepancies. Maybe you could implement a getter for it as well, to handle the situation.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175