Instantiating interfaces in Java

前端 未结 14 1580
南笙
南笙 2020-12-07 21:08

I have this interface:

public interface Animal {
    public void Eat(String name);
}

And this code here implements the interface:



        
14条回答
  •  佛祖请我去吃肉
    2020-12-07 21:32

    No it is not - you are instantiating a Dog, but since a Dog is an Animal, you can declare the variable to be an Animal. If you try to instantiate the interface Animal it would be:

    Animal baby2 = new Animal();
    

    Try that, and watch the compiler scream in horror :)

提交回复
热议问题