top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can interfaces in Java contain inner classes if yes then describe it, if no then why not?

0 votes
403 views
Can interfaces in Java contain inner classes if yes then describe it, if no then why not?
posted Jan 31, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Yes it is possible to have static class definitions inside an interface, but maybe the most useful aspect of this feature is when using enum types (which are special kind of static classes). and one case where I've found it useful is when an interface has to throw custom exceptions. You the keep the exceptions with their associated interface, which I think is often neater than littering your source tree with heaps of trivial exception files.

interface MyInterface {

   public static class MyInterfaceException extends Exception {
   }

   void doSomething() throws MyInterfaceException;
}
answer Jun 29, 2017 by Ajay Kumar
...