Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing an Interface?

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 information from the javadoc of each class used.

Now, I pulled quite a bit of this code from previous questions so there was a fair bit I didn't write myself. The issue I noticed is that the code I grabbed is initializing an object, say "SimpleDoc" which implements an interface (Doc) and assigning it to that interface?!

Little Code snippet:

Doc mydoc = new SimpleDoc(textStream, flavor, null);

Now as far as I was led to understand in java we create objects. I'm familiar with inheritance, and I'm familiar with the trick of using interfaces to allow a class to "inherit" multiple super classes.

But this just isn't sticking right. You can create a class which implements an interface, that is fine with me. But what is happening here when a interface is created and an object is reduced to its interface? What am I accessing when I reference mydoc exactly?

like image 517
Alex Avatar asked Jan 12 '12 20:01

Alex


2 Answers

The trick is to realize that you're not "creating", "instantiating", or "initializing" an interface. You are simply defining a variable as being something that you know implements that interface.

You are essentially telling other programmers working on this code that for the rest of this method, you are only interested in the fact that myDoc is a Doc (i.e., something that satisfies the Doc interface). This can make programming simpler because the IDE's auto-complete will now only show you the methods that are defined by this interface, rather than everything that a SimpleDoc is capable of doing.

Imagine that in the future you want to expand your functionality so that you could have different implementations of Doc depending on some input. Rather than creating the SimpleDoc explicitly, you say:

Doc mydoc = docFactory.getByType(inputType);

The docFactory can produce any type of Doc, and this method doesn't really care what kind gets instantiated, because it's going to treat it like a Doc regardless.

like image 61
StriplingWarrior Avatar answered Sep 23 '22 16:09

StriplingWarrior


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()

like image 41
Eike Cochu Avatar answered Sep 24 '22 16:09

Eike Cochu