Q. Can interfaces contain inner classes? A. Yes, but not - TopicsExpress



          

Q. Can interfaces contain inner classes? A. Yes, but not recommended as it can compromise on clarity of your code. The interface with an inner class for demo purpose only? package innerstatic; public interface InterfaceWithInnerClass { public static final Util UTIL = new Util( ); public String getName(); // next node in this list public final static InterfaceWithInnerClass FIELD = //anonymous inner class as the interface field declaration new InterfaceWithInnerClass() { public String getName() { return "Peter"; } }; //static inner class public static class Util { public String getDetail(){ return FIELD.getName() + " age 25"; } } } The class that implemnts the above interface.? package innerstatic; public class Test implements InterfaceWithInnerClass { @Override public String getName() { return FIELD.getName() + " : " + UTIL.getDetail(); } public static void main(String[] args) { System.out.println(new Test().getName()); } } The output is: Peter : Peter age 25
Posted on: Tue, 30 Jul 2013 11:33:24 +0000

Trending Topics



Recently Viewed Topics




© 2015