Instantiating interfaces in Java

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

    Dog is not an interface: Dog is a class that implements the Animal interface.

    There's nothing untoward going on here.


    Note that you can instantiate an anonymous implementation of an interface, like so:

    Animal animal = new Animal() {
        public void Eat(String food_name) {
            System.out.printf("Someone ate " + food_name);
        }
    };
    

提交回复
热议问题