Can Interface be defined inside class

Can Interface be defined inside class

Answer: Yes we can define interface inside class according to java but defining interface inside class will make very tight coupling between class and interface. It’s better to have this separately to keep object work independently. Please see example below:

package com.javahonk;

public class InterfaceInsideClass {

    interface TestInterfaceInsideClass {

	void demoMethodInsideClass();
    }

}

 

Interface implementation:

package com.javahonk;

public class ImplementInterfaceInsideClass implements
	InterfaceInsideClass.TestInterfaceInsideClass {

    public static void main(String[] args) {

	ImplementInterfaceInsideClass insideClass = 
		new ImplementInterfaceInsideClass();
	insideClass.demoMethodInsideClass();

    }

    @Override
    public void demoMethodInsideClass() {
	System.out.println("Interace Method implementation");

    }

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *