The interface inside the class

What is the difference between using an interface inside a class, inside a nested class and an outer class.

As I read about the class DataStructure.java

in Questions and Exercises: Nested Classes in Oracle (insert example snippet here):

public class DataStructure {
//some code
    interface DataStructureIterator extends java.util.Iterator<Integer> { } 

    // Inner class implements the DataStructureIterator interface,
    // which extends the Iterator<Integer> interface

    private class EvenIterator implements DataStructureIterator {
//rest code

      

  • So, as the code shows, there is no body in the interface. Can't I just extend the EvenIterator class with java.util.Iterator<Integer>

    this interface instead and implement it?

  • Is there any other difference (besides reading the code) between declaring interference outside / inside a class?

  • What happens when the outer class is extended by the interface. Will this affect the nested class in any way?

I just want to be confident about these things so I know how to use them correctly, thanks for your time.

+2


source to share


1 answer


  • Since the code shows that there is no body in the interface. Can't I just extend the EvenIterator class with java.util.Iterator instead of creating this interface and implementing it?

Yes, you could. But this method can be more readable and extensible. Even if there are no members now, they can be added later.

  1. Is there any other difference (besides reading the code) between declaring an interference outside / inside a class?

The nested interface is implicitly static, so the only effect is that the nested interface is part of the host-space namespace.

Since class members can be declared as protected

or private

, this also applies to nested interfaces. However, it is rarely useful to use interfaces private

because they can only be implemented in one class, so why bother with interfaces at all? However, interfaces protected

can be helpful. For example, you might have an abstract factory method that is used by subclasses to provide instances to the parent class. Here's a contrived example:

public abstract class Enclosing {

    protected interface JobHandler {
        void handle(Job job) throws JobException;
    }

    protected abstract JobHandler createJobHandler();

    // public methods omitted

    private void doTheJob(Job job) {
        createJobHandler().handle(job);
    }
}

      



If the interface is declared package-private, it can just be at the package level. The only reason you'll want to squeeze it into a class is because it's very closely related to the class itself. Perhaps it is some kind of helper interface that is strictly used in unit testing this particular class.

If it's an interface public

, then it's usually a bad idea to nest it. Because by doing this, you increase the communication between the interface and the enclosing class. And interfaces are one of the best ways to reduce grip! So why waste your potential?

Suppose you have a library mylib-buttons

that has a class Button

. One day off Button.ClickListener

seems like a good idea. Then you want to reuse that interface in another class, and perhaps even in another library. But you cannot do this without introducing a (possibly unnecessary) dependency on the library containing the class Button

. On the other hand, if it's a top-level interface, then you just pull the interfaces into another library, say mylib-core

, leaving only the messy buttons in mylib-buttons

.

Nested interfaces within interfaces are a slightly different story. They can be part of the same design and are meant to be used together. @ cricket_007 one comment gives a good example Map.Entry

.

  1. What happens when the outer class is extended by the interface. Will this affect the nested class in any way?

This is not entirely clear. How can a class be extended by an interface? That said, no matter what you mean here, you can probably answer it yourself when you consider the aforementioned fact: a nested interface is just part of the namespace of the classspace and what it is. There are no other consequences.

+1


source







All Articles