Why is class B {} not a correct answer in this case? The explanation discusses the fact that class A does not have a no-arg constructor, but nowhere in the question does it say that class B needs to extend class A. It asks "Which of the following would be a valid inner class for this class?".
This seems to compile just fine. Am I misreading the question?
[1.] right:
class B {}
[2.] wrong:
class B extends A {}
//Note that A doesn't have any no-args constructor, so there is no way B's object can be created.
[3.] right:
class B { B() { System.out.println("i = " + i); } }
[4.] wrong:
class B { class A {} }
//Inner class cannot have same name as the enclosing class.
[5.] wrong:
class A {}
//Inner class cannot have same name as enclosing class.
But the explanation is still wrong, because there is no Inheritance involved.
//Now, since class A does not define a no-argument constructor the above code will not compile.
It is interesting to know that inner class B can extend its enclosing class A (option 2 is wrong, but another reason is given). Because (haven't tested yet, but thinking aloud) if that were the case it this should make the following possible:
B myB = new B.new B.new B.new B.new B(); //...arbitrary number of recursions
Is exteding enclosing class by an inner class really possible and how would that be useful in java?