In the following program I have use "Bases" as outer class and "AnonymousCos" as inner class. It helps constructor to define any method. It is very easy process to create an inner class in java. Just try to understand following example. It is very simple.
Code:
abstract class Bases {
public Base(int k) {
System.out.println("Base constructors, k = " + k);
}
public abstract void fs();
}
public class AnonymousCos {
public static Base getBase(int k) {
return new Bases(k) {
{
System.out.println("Inside another method");
}
public void fs() {
System.out.println("In anonymous fs()");
}
};
}
public static void main(String[] args) {
Base bases = getBase(32);
bases.fs();
}
}
Bookmarks