3

Can we have a class inside an interface which has different methods of the interface implemented in it. I have a doubt here that why Java allows writing Inner classes inside interfaces and where can we use it.

In the program below I have written a class inside Interface and implemented the methods of the interface. In the implementation class of the interface I have just called the inner class methods.

public interface StrangeInterface
    {
      int a=10;int b=5;
      void add();
      void sub();
      class Inner
       {
         void add()
          {
             int c=a+b;
             System.out.println("After Addition:"+c);
          }
         void sub()
         {
             int c=a-b;
             System.out.println("After Subtraction:"+c);
         }
       }
    }   

 abstract public class StrangeInterfaceImpl implements I { 
      public static void main(String args[])
    {
       StrangInterface.Inner i=new StrangeInterface.Inner();
       i.add();
       i.sub();
    }
 }
Jeremy Johnson
  • 469
  • 1
  • 4
  • 17
Niketh Kumar
  • 159
  • 1
  • 2
  • 8
  • Interfaces aren't meant to be executed. They are tools that are implemented into other classes like an ActionListener, for example. An ActionListener cannot be instantiated, it can only be used with components. An interface basically lends its methods to a class so that it can perform special actions. – Jeremy Johnson Aug 19 '13 at 18:33
  • @JeremyJohnson: I know the theory behind using of interfaces but here there is no strict rule as such provided by java. – Niketh Kumar Aug 19 '13 at 18:38
  • @Krishna: I agree, but what 'if' somewhere there is a occurance – Niketh Kumar Aug 19 '13 at 18:39
  • @Antoniossss: It compiled and produced result. – Niketh Kumar Aug 19 '13 at 18:39
  • @NikethKumar what would be the point of such a thing? It seems like you're working harder to go around a rule than to accomplish something. But I could be wrong. What are you trying to accomplish? – Jeremy Johnson Aug 19 '13 at 18:40
  • 3
    @Krishna Oh? Why not? Works fine for me. – Dave Newton Aug 19 '13 at 18:41
  • My question is why have a main method inside of an interface? If the interface is being implemented by a class, the main method would have instantiated that class or the one that instantiated it, etc. Wouldn't it just be simpler to have a class with a main method as opposed to working out using an interface for it? – Jeremy Johnson Aug 19 '13 at 18:45
  • @JeremyJohnson How's that related to the question? Or did you mean a `main` method in an abstract class, which is something different? – Dave Newton Aug 19 '13 at 18:46
  • @DaveNewton yes, in a way that's what I'm wondering. I haven't seen it done like that before and I am now curious about the benefits of such a thing. – Jeremy Johnson Aug 19 '13 at 18:49
  • 2
    @JeremyJohnson A class doesn't have to be instantiated to call a static method on it; while I've never actually *done* such a thing or seen it in real life, it could be used as the entry point into code that implements a factory of child classes, as a way to enforce non-instantiability, and so on. – Dave Newton Aug 19 '13 at 18:52
  • 1
    @JeremyJohnson: I was just trying with Inner classes inside interfaces not to accomplish anything. The point here is that as with the definition of an Interface, it should have only abstract methods and final variables. But java supports for classes as members of Interface. I just want to make clear the point that what can be the use of an Innerclass inside Interface – Niketh Kumar Aug 19 '13 at 18:52
  • @NikethKumar I see that now, and it's pretty clever. Very interesting technique. – Jeremy Johnson Aug 19 '13 at 19:00

4 Answers4

5

You can define a class inside an interface. Inside the interface, the inner class is implicitly public static.

From JLS Section 9.1.4:

The body of an interface may declare members of the interface, that is, fields (§9.3), methods (§9.4), classes (§9.5), and interfaces (§9.5).

From JLS Section 9.5:

Interfaces may contain member type declarations (§8.5).

A member type declaration in an interface is implicitly static and public. It is permitted to redundantly specify either or both of these modifiers.

The only restriction on the inner class defined inside the interface or any other class, for that matter, is that, you have to access them using the enclosing member name.
Apart from that, there is no relation between them. The inner class will result in completely a different class file after compilation.

For e.g., if you compile the following source file:

interface Hello {
    class HelloInner {

    }
}

Two class files will be generated:

Hello.class
Hello$HelloInner.class
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2
Can we have a class inside an interface which has different methods of the interface implemented in it.

IMHO But interfaces are not meant to for that purpose.

If you write inner class in an interface it is always public and static.

It's equivalent to

public interface StrangeInterface
    {
 public static class Inner{

}

and the variable inside the interface also explicitly public static variables.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

An interface might provide its own implementation as a default.

Note that unless you declare the inner class implements the interface, there's no relation between the two other than it's an inner class. When a class is very tightly related to the interface this isn't intrinsically unreasonable, although I'd be suspicious it's a generally-useful pattern.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • @ Dave: I didnt get this "When a class is very tightly related to the interface this isn't intrinsically unreasonable, although I'd be suspicious it's a generally-useful pattern" – Niketh Kumar Aug 19 '13 at 19:22
  • @NikethKumar A class that isn't implementing the interface, but is tightly tied to the interface, might make some sense in this pattern. For example, `Map.Entry` might have been a default implementation of a map entry. (It's not; it's another interface.) – Dave Newton Aug 19 '13 at 19:25
0

to summarize "where can we use it" by defining a class inside an interface:
1. to provide default implementation for an interface
2. if argument or return type for interface method/s is class

w.r.t your code

    interface StrangeInterface {
    int a = 10;
    int b = 5;

    void add();

    void sub();

    class Inner implements StrangeInterface {
        public void add() {
            int c = a + b;
            System.out.println("After Addition:" + c);
        }

        public void sub() {
            int c = a - b;
            System.out.println("After Subtraction:" + c);
        }
    }
}

class MyTest implements StrangeInterface {

    public void add() {

        System.out.println("My own implementation for add : " + (a +b));
    }

    public void sub() {
        System.out.println("My own implementation for sub : " + (a- b));

    }

}

public class StrangeInterfaceImpl {

    public static void main(String args[]) {
        StrangeInterface.Inner i = new StrangeInterface.Inner(); // calling default implementation
        i.add();
        i.sub();

        MyTest t = new MyTest();   // my own implementation
        t.add();
        t.sub();
    }
}
irfan
  • 878
  • 9
  • 10