About Question com.enthuware.ets.scjp.v6.2.530 :

Moderator: admin

Post Reply
KevinC
Posts: 1
Joined: Fri Aug 09, 2013 12:28 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.530 :

Post by KevinC » Mon Aug 12, 2013 11:48 pm

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{}
}

patpat09
Posts: 2
Joined: Mon Nov 11, 2013 3:16 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.530 :

Post by patpat09 » Mon Nov 11, 2013 3:25 pm

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.

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.530 :

Post by admin » Mon Nov 11, 2013 4:58 pm

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.
If you like our products and services, please help us by posting your review here.

piotrkmiotczyk
Posts: 27
Joined: Mon Sep 22, 2014 1:30 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.530 :

Post by piotrkmiotczyk » Sun Oct 05, 2014 12:02 pm

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?

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.530 :

Post by admin » Sun Oct 05, 2014 8:21 pm

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.
If you like our products and services, please help us by posting your review here.

piotrkmiotczyk
Posts: 27
Joined: Mon Sep 22, 2014 1:30 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.530 :

Post by piotrkmiotczyk » Mon Oct 06, 2014 4:44 am

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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests