The B class is an non-static inner class of Class A. It must have an enclosing instance of A in order to exist. Consider it like a variable: If B were a non-static variable in a you would need to have an instance of A in order to access it. The same goes with non-static inner classes - they can't exist without the outer class.
So when you try to extend the inner class B from C, you are trying to make it possible to create an instance of B without a surrounding instance of A, which is impossible.
The inner class B actually has access to the enclosing-classes members, so if A had a private method or member variable B may be using it. Without an enclosing instance of A, how would class C use A's member methods and variables?
You cannot extend B this way unless B is static or C is also an inner class of A. As you have a constructor for A, which means you need to instantiate A to access non-static fields of A but again you cannot use instances to define a class. That says ,even if you create C as an inner class of some other class D and then try to define C as "C extends a.B" where a is an instance of A then it will say "a" cannot be resolved as a type (where "type" is a keyword, and an instance is not a type)
If you still want to inherit B and not A you will have to do it this way
Just to warn you, I am a beginner in Java, so don't believe a word I say. I thought this might help though:
In your original code, you tried to call the demo method by using
But, demo() is the inner class B's method!
Although class C does inherit an inner class following the blueprint from class A, you still need to create an instance of class B ( a 'B' object ). That object then gets created and that object can use IT'S demo() method as I've shown below.
Hope this helps :-)
Joe [ December 14, 2008: Message edited by: Joe Lemmer ]