简体   繁体   中英

Why can variables be assigned to interface type?

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);
            }
        } 
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM