Monday 25 March 2013

Creating class inside Interface

Recently i came to know that it is possible to create a class inside a interface. Its just for those people who don't know it is possible.

First we will be creating one interface as below.

TestClassinsideInterface.java

package com.lnt.test;

public interface TestClassinsideInterface {
 void dofoo();
 Ifoo ifoo=new Ifoo();

 final class Ifoo implements TestClassinsideInterface{

    @Override
    public void dofoo() {
        // TODO Auto-generated method stub
        System.out.println("inside dofoo");
    }
   
 }
}


Now creating class with main method to access the class inside interface.


TestInnerInterfaceClass.java


package com.lnt.test;

public class TestInnerInterfaceClass {

   
    public static void main(String[] args){
        TestClassinsideInterface testInterface=TestClassinsideInterface.ifoo;
        testInterface.dofoo();
    }
}