1

Interface provide 100% abstraction it has methods that don't have implementation to fulfill there abstract behavior but here i have one example which force me to think of abstraction capability of Interface.

i have interface and class inside it and it has concrete method and i can access that method in another class

Example:-

interface InnerInterFaceTest1 {
     class InnerClass {
        public  void doWork() {
            System.out.println("in interface innner class");
        }
    }
}
public class InnerInterFaceTest implements InnerInterFaceTest1 {
    public static void main(String... a) {
        InnerInterFaceTest1.InnerClass class1 = new InnerInterFaceTest1.InnerClass() ;
        class1.doWork();
    }
}

concept of interface having inner class does this???or else what that make sense??

Nirav Prajapati
  • 2,987
  • 27
  • 32

4 Answers4

1

The JLS defines a nested class as follows:

A nested class is any class whose declaration occurs within the body of another class or interface.

Note that a nested class declared in an interface is implicitly public and static.

So this is actually not an inner class, but more of a top-level class that's packaged or nested in a another top-level type.

M A
  • 71,713
  • 13
  • 134
  • 174
1

Class declarations inside an interface are implicitly static so they are nested classes but not real inner classes, i.e. instances of them have no reference to instances of the outer interface.

So you have a concrete class InnerInterFaceTest1.InnerClass which has a kind of name space relationship to the interface InnerInterFaceTest1, however, there are no other consequences to the abstraction, specifically for your question, the interface does not become less abstract due to the existence of that nested class. It would be no different than if someone created a top-level class and named it InnerInterFaceTest1_InnerClass, for example.

Holger
  • 285,553
  • 42
  • 434
  • 765
1

Interface class notify java compiler that treat this class as interface, So, java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members and nothing else.

So, If you write subclass in interface class is same as you write subclass in class.

As per this theory you can do every process for subclass which you can do for normal class.

bharatpatel
  • 1,203
  • 11
  • 22
0

It is not common but sometimes you need specify structures to be used in your interface and those classes can have e.g. consistency primitives. See an example at https://stackoverflow.com/a/10481539/4054054

Community
  • 1
  • 1
Jenda
  • 11
  • 2