java - How interface support multiple inheritance -


this question has answer here:

public  class test implements x, y { //x.y interface 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 me solve issue

 interface x {        public void mymethod();        int = 0;  }   interface y {        int = 9;        public void mymethod();  } 

any variable defined in interface is, definition, public static final, in other words it's constant, it's not field (since there no fields in interfaces).

so compilation error points out compiler doesn't know witch constant refer to.

you have 2 options here:

  1. change name of constant in 1 of interfaces, example in interface y declare int b = 9;

  2. inside main method point concrete constant: system.out.println(x.a);


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -