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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

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 »

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 »

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: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
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 »

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: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
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 »

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: Google [Bot] and 25 guests