I generally understand the various flavours of inner class. I have found the below example which is causing me some confusion. The point of confusion is that the main class (AbstractTest) is abstract. This usually means the class cannot be instantiated and that the first concrete subclass must fully implement any previously declared abstract methods. In this case there is also an abstract inner class. When anonymous class one extends AbstractTest this compiles okay even though this anonymous class does not implement the abstract inner class. Where as if there is a regular abstract method (doStuff) in AbstractTest and anonymous class one does implement, then receive error saying abstract method doStuff does not exist. The meaning of abstract seems to not be the same when applied to an inner class as a method. What does an abstract inner class really mean? Does it only mean you must subclass it too through either another inner class or anonymous class? Apologies if this is not clear, I am starting to go round in circles with the inner class variations.
public abstract class AbstractTest
{
// public method
public int getNum()
{
return 45;
}
// public inner class
public abstract class Bar
{
// public inner class method
public int getNum()
{
return 38;
}
}
// public abstract void doStuff(); // If uncomment this then receive error
// because anonymous class does not
// implement this method.
// main method
public static void main(
String args[])
{
// anonymous class one
AbstractTest t = new AbstractTest()
{
// anonymous class overriding method
public int getNum()
{
return 22;
}
};
// anonymous class two
AbstractTest.Bar f = t.new Bar()
{
// anonymous class overriding method
public int getNum()
{
return 57;
}
};
System.out.println(f.getNum() + " " + t.getNum());
} // end method main
} // end class