4

I am reading about what an interface contains and I understand in addition to the usual they can also contain inner classes or other interface.

Can anyone explain why or for what purpose anyone would want to put an interface inside an interface. Also why would anyone put an inner class inside. I have looked on the net but not found any good explanations other than a link that points to one of the standard java classes. What I really need to help me understand is a simple example.

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 2
    I think it is just a grouping thing (to show that these interfaces only work together). – Thilo Jun 14 '12 at 10:07
  • I'd assume you'd include an inner class for the same reason you include fields and methods in an interface; you want to make a guarantee about what a certain class will implement, without worrying about the exact details of the implementation. I'm not sure about interfaces, though. – Anthony Grist Jun 14 '12 at 10:09
  • Related: [this](http://stackoverflow.com/questions/1482158/inner-interfaces) and [this](http://stackoverflow.com/q/71625/211197) – maksimov Jun 14 '12 at 10:14

6 Answers6

5

Here's an excerpt from an interface of mine that uses an inner class:

public interface EmailService {

    void send(EmailDetails details);

    class EmailDetails {
        private String from;
        private List<String> to = Lists.newArrayList();
        private String messageTemplate;
        //...
    }
}

You see - the point is that the interface needs some additional definition (in this case - the parameter its method accepts). It would be the same thing with an inner interface.

It could've been a separate class/interface, but as it is only relevant to this interface, I put it there.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I added a paragraph about that – Bozho Jun 14 '12 at 10:13
  • How can this class be relevant to this interface only? Surely there'd be classes that a) implement the interface and b) call the #send(EmailDetails) - thus must construct or handle EmailDetails? – maksimov Jun 14 '12 at 10:16
  • they only construct EmailDetails for use with `EmailService`. It makes no sense without `EmailService` being used. – Bozho Jun 14 '12 at 10:17
  • @Bozho why? And what if you want to save your emails in DB? – Dmitry Zaytsev Jun 14 '12 at 10:23
  • If you want to, it will be a separate class. If you don't (like in my case), it can be an inner class. – Bozho Jun 14 '12 at 10:25
2

You do not pollute the namespace with things that are only related to one particular class or interface.

Inner classes (if not static), however, are much more relevant than inner interfaces, since they contain a hidden link to an instance of the containing class; thus instances of an inner class are always implicitly bound to an instance of the outer class:

class OuterClass {

   int someField;

   class InnerClass {

      void doSomething () {
         someField += 1; // you can access someField from here
      }

   };

   void methodUsingInnerClass () {
      InnerClass obj = new InnerClass (); // obj knows to which instance of OuterClass it belongs
      someField = 0;
      obj.doSomething ();
      // now someField == 1
   }
};

This is used often in combination with anonymous classes (which are inner classes by definition), e.g. to provide event handlers.

JohnB
  • 13,315
  • 4
  • 38
  • 65
1

To achieve strong Composition relationship we go for inner classes.

EDIT:

A Man has heart So when Man's object is constructed at the same time heart's object is constructed.Heart objects scope has no meaning out of man's body so Heart class should be inner class of Man class.

amicngh
  • 7,831
  • 3
  • 35
  • 54
1

Since Java doesn't have multiple inheritance, inner classes can be very handy. For example:

class Vehicle{
}

class Transporter{
}

//you cant create class like this
//class Car extends Vehicle, Transporter {}

//but you can create something like that
class Car extends Vehicle{

    class InnerTransporter extends Transporter{}

}

It's not a perfect example (why Transporter doesn't extends Vehicle?), but I hope you got the idea.

And what about inner interfaces. For example

class View{

    public static interface OnClickListener{ //this interface associated with View, so it's more readable
        public void onClick();
    }

}
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
1

It's been tackled before, and let me quote one of the good reasons for this:

one good reason to use an inner interface is if its function is directly related to the class [or interface] it is in. A good example of this is a Listener. If you had a class [or interface] Foo and you wanted other classes [or interfaces] to be able to listen for events on it, you could declare an interface named FooListener, which is ok, but it would probably be more clear to declare an inner interface and have those other classes [interfaces] implement Foo.Listener

Community
  • 1
  • 1
JWL
  • 13,591
  • 7
  • 57
  • 63
1

A sample:

http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html

I guess because the entry interface is really tightly related to Map, the API designers decided to make Entry an inner interface of Map rather than a top level interface.

In practice inner interfaces are rather the exception than the rule, though.

Puce
  • 37,247
  • 13
  • 80
  • 152