Two interfaces with same method signature implemented in Java class

后端 未结 7 1500
独厮守ぢ
独厮守ぢ 2020-12-05 08:12

I have two Java interfaces and one implementing class.

(I have used Eclipse to run the program directly, and I did not try to check any compiler warning et cetera by

7条回答
  •  误落风尘
    2020-12-05 08:31

    In this situation, there is no issue because both interfaces have same method signature. But What about this ?

    interface Animal {
        public void eat() throws IOException;
    }
    
    interface Plants {
        public void eat() throws NullPointerException;
    }
    

    Which one is choosen by the compiler ? Why does it get error below code ?

    public class Test implements Animal, Plants {
    
        public void eat() throws IOException {
    
        }
    }
    

    Compiler says : Exception IOException is not compatible with throws clause in Plants.eat()

提交回复
热议问题