Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.530 :
Posted: Mon Aug 12, 2013 11:48 pm
by KevinC
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?
Code: Select all
class A
{
protected int i;
public A(int i)
{
this.i = i;
}
class B{}
}
Re: About Question com.enthuware.ets.scjp.v6.2.530 :
Posted: Mon Nov 11, 2013 3:25 pm
by patpat09
In Teststudio v3.35 build 20130831 this question/answer ist fixed.
The current answers are:
Code: Select all
[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.
Re: About Question com.enthuware.ets.scjp.v6.2.530 :
Posted: Mon Nov 11, 2013 4:58 pm
by admin
Hi,
Not sure which explanation you think is wrong. Can you please more specific so that we can help?
Option 2 does involve inheritance.
-Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.530 :
Posted: Sun Oct 05, 2014 12:02 pm
by piotrkmiotczyk
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?
Re: About Question com.enthuware.ets.scjp.v6.2.530 :
Posted: Sun Oct 05, 2014 8:21 pm
by admin
piotrkmiotczyk wrote:
Is exteding enclosing class by an inner class really possible
Yes, you might want to try it out
piotrkmiotczyk wrote:
and how would that be useful in java?
That is subjective. I can't think of any but I am sure the designers of the language had their reasons for allowing it.
HTH,
Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.530 :
Posted: Mon Oct 06, 2014 4:44 am
by piotrkmiotczyk
Yup. Just to follow up, apart from basic errors I made in the post above, the following is possible.:
A.B myB = new A().new B().new B().new B().new B().new B().new B(); //... etc.
I ran out of patience after a few thousand columns of nesting, though it does throw an error at some point (after extensive copy paste use).
A.B.B.B.B.B.B myB2 = new A().new B();
Also possible. The compiler just seems to ignore all the generations of B in between enclosing class A and whichever B the reference points to.