سؤال

interface A
{
  int a = 10;
}

interface B
{
  int a = 10;
}

class Access implements A, B
{
}

class Demo
{
  public static void main(String args[])
  {
    Access ac = new Access();
    System.out.println(ac.a);   // line #1
  }
}

Line #1 causes ambiguity. How do I remove it? Is it not possible to have same variable name of different interfaces?

هل كانت مفيدة؟

المحلول

You will have to reference the two variables as A.a and B.a respectively;

نصائح أخرى

System.out.println( ((A)ac).a);   

or
System.out.println( ((B)ac).a); 

If you have variables which are conflicting then you need to use resolution to access value of particular interface.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top