Instantiating interfaces in Java

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

    You can't instantiate an interface. The functionality can be considered similar to that of an abstract class. You can have a reference to the interface but you don't create an object of interface. If you do something like this....

    Animal a = new Animal(); The compiler will show an error- "Cannnot instantiate the type Animal".

提交回复
热议问题