Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would anyone want to put an interface in an interface in Java?

Tags:

java

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.

like image 317
Alan2 Avatar asked Dec 21 '22 20:12

Alan2


1 Answers

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.

like image 51
Bozho Avatar answered Apr 29 '23 09:04

Bozho