Why can variables be assigned to interface type?

时光总嘲笑我的痴心妄想 提交于 2020-01-07 03:03:13

问题


I'm new to Java and am trying to learn the concept of interface. I saw the code below online. I understand that interface can't be instantiated. My question is, WatchService, Path, WatchKey and WatchEvent are all interface, how come variables can be assigned to interface type? Is is the same as instantiation?

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class WatchServices {

    public static void main(String[] args) throws IOException {
        WatchService ws1 = FileSystems.getDefault().newWatchService();

        Path p1 = Paths.get("/Users/justin/Desktop/Codes Netbean/JavaRandom");

        WatchKey wk1 = p1.register(ws1, ENTRY_CREATE);

        while(true){
            for(WatchEvent<?> event : wk1.pollEvents()){
                System.out.println(event.kind());
                Path file  = (Path)event.context();
                System.out.println(file);
            }
        } 
    }
}

回答1:


I'll answer you with an example.

Suppose that I ask you to bring me a sphere. You then bring me a beach ball.

A sphere doesn't exist, it's a concept, a geometrical object and a beach ball is a material object which can be considered a sphere.

That's approximately the same thing, an interface defines the characteristics of an object through its exposed interface (the methods that can be called on an object which pretend to implement that interface) but it's not a material object per se.

So I can consider a beach ball as a sphere even if the sphere doesn't exist. It doesn't make any sense to instantiate a sphere, since a sphere can't be instantiated, but I can treat a beach ball as a sphere.




回答2:


An interface defines a set of methods that can be called, but does not implement them. That is why you can never say Runnable x = new Runnable(). A class can be defined, which implements all the methods in the interface. It also has to declare that it implements that interface:

interface Runnable {
    public void run();
}

public class RandomTask implements Runnable {
    public void run() {
        System.out.println("Doing some stuff...");
    }

    public void shout() {
        System.out.println("DOING STUFF...");
    }
}

See how the class implements the interface. So a RandomTask is runnable, and therefore you can assign a RandomTask where a Runnable was expected:

Runnable x = new RandomTask();

The interface tells you what methods must exist on that object. We know that x must have a run() method, because it is a Runnable. So we can say

x.run();

Now, while you the programmer know that x has a shout() method, you can't access it anymore. By assigning x to a Runnable, you limit it to have only the methods that are defined for a Runnable.



来源:https://stackoverflow.com/questions/35352254/why-can-variables-be-assigned-to-interface-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!