Initializing an Interface?

前端 未结 2 1652
野的像风
野的像风 2020-12-16 02:09

In a current problem I am having (printing a file to a physical printer in Java) I have been running through the code like a madman trying to devour any useful missed inform

2条回答
  •  时光说笑
    2020-12-16 02:43

    You can not create interfaces, what you do here is you create an object mydoc of the class SimpleDoc which implements the interface Doc. Because the class implements this interface, you are allowed to handle mydoc as if it was an instance of that interface. This allows you to access all declared methods in the interface, which are implemented in the class SimpleDoc

    If, for example, your Doc-Interface would look like this:

    public interface Doc {
        void print();
    }
    

    and your SimpleDoc class would look like this:

    public class SimpleDoc implements Doc {
    
        public void clear() { ... }
    
        @Override
        public void print() { ... }
    
    }
    

    ... then you could only access the print()-method of you mydoc-object. But you could also say:

    SimpleDoc mydoc = new SimpleDoc();
    

    ... and then you would be also able to call clear()

提交回复
热议问题