interface cannot be instantiation

1)
public interface Handler{
public void Hello();

2)
import Handler;
public class OtherParser{
Handler handler;
……
}

2) just uses a declaration of the interface, which is different from instiation

接口可以看成是高度抽象的抽象类,它描述的事物们所共有的方法(方法签名),也就是规定除了该接口的方法的调用参数与规则,仅仅而已,它的使用必须依赖于实现类。
例如:
public class MyHandler implements Handler{
public void Hellp(){
System.out.println(“my Handler implements”);
}
}
而在对接口的引用时,采用的是实例化实现该接口的类
Handler handler = new MyHander();

 

In the case below, Animal is an interface. it uses an anonymous class which is actually a class; that is why it can be instantiated.

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

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s