Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner class within Interface

Is it possible to create an inner class within an interface?
If it is possible why would we want to create an inner class like that since we are not going to create any interface objects?

Do these inner classes help in any development process?

like image 617
gmhk Avatar asked Mar 08 '10 11:03

gmhk


People also ask

Can we have inner class in interface?

Yes, you can define a class inside an interface.

How do you declare a class inside an interface in Java?

Yes, you can define an interface inside a class and it is known as a nested interface. You can't access a nested interface directly; you need to access (implement) the nested interface using the inner class or by using the name of the class holding this nested interface.

Can we implement class in interface in Java?

A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.

Can we use abstract class inside interface?

This is a class that usually contains at least one abstract method which can't be instantiated and It is also possible for the class to have no methods at all. The instance of an abstract class can't be created. Now as all methods in an interface are abstract methods therefore we can implement it using Abstract Class.


2 Answers

Yes, we can have classes inside interfaces. One example of usage could be

public interface Input {     public static class KeyEvent {          public static final int KEY_DOWN = 0;          public static final int KEY_UP = 1;          public int type;          public int keyCode;          public char keyChar;     }     public static class TouchEvent {          public static final int TOUCH_DOWN = 0;          public static final int TOUCH_UP = 1;          public static final int TOUCH_DRAGGED = 2;          public int type;          public int x, y;          public int pointer;     }     public boolean isKeyPressed(int keyCode);     public boolean isTouchDown(int pointer);     public int getTouchX(int pointer);     public int getTouchY(int pointer);     public float getAccelX();     public float getAccelY();     public float getAccelZ();     public List<KeyEvent> getKeyEvents();     public List<TouchEvent> getTouchEvents(); } 

Here the code has two nested classes which are for encapsulating information about event objects which are later used in method definitions like getKeyEvents(). Having them inside the Input interface improves cohesion.

like image 200
zafar142003 Avatar answered Sep 23 '22 21:09

zafar142003


Yes, you can create both a nested class or an inner class inside a Java interface (note that contrarily to popular belief there's no such thing as an "static inner class": this simply makes no sense, there's nothing "inner" and no "outter" class when a nested class is static, so it cannot be "static inner").

Anyway, the following compiles fine:

public interface A {     class B {     } } 

I've seen it used to put some kind of "contract checker" directly in the interface definition (well, in the class nested in the interface, that can have static methods, contrarily to the interface itself, which can't). Looking like this if I recall correctly.

public interface A {     static class B {         public static boolean verifyState( A a ) {             return (true if object implementing class A looks to be in a valid state)         }     } } 

Note that I'm not commenting on the usefulness of such a thing, I'm simply answering your question: it can be done and this is one kind of use I've seen made of it.

Now I won't comment on the usefulness of such a construct and from I've seen: I've seen it, but it's not a very common construct.

200KLOC codebase here where this happens exactly zero time (but then we've got a lot of other things that we consider bad practices that happen exactly zero time too that other people would find perfectly normal so...).

like image 32
SyntaxT3rr0r Avatar answered Sep 22 '22 21:09

SyntaxT3rr0r