These days I was asked to do some screen code in an interview. The class the interview provided is very impressive. It is an interface with an inner class. Honestly I have used Java for many years, but I seldom put inner class in an interface. To me, an interface is an interface, it should not contain any implementations. If you want to contain some implementations, why use interface?AbstractClass is a better candidate.

Anyway below is the sample code

interface MyInterface{
public void printme();
static final class MyInnerClass implements MyInterface {
@Override
public void printme() {
System.out.println(“MyInnerClass”);
}
}
}

How to use the above interface?

MyInterface.MyInnerClass m=new MyInterface.MyInnerClass();
m.printme();

As expected a line “MyInnerClass” is printed out in the console.

If I were the designer I’s rather separate the inner class from the interface.

Visitors, your thoughts?

Advertisements