Instantiating interfaces in Java

前端 未结 14 1586
南笙
南笙 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:11

    Animal baby2 = new Dog(); //HERE!!!!!!!!!!!!!!!!!!!!!!
    

    Surely you are not instantiating the Animal. You are only referring the Dog instance to it. In java we can take the super class reference.

提交回复
热议问题