About Question enthuware.ocpjp.v7.2.1580 :

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

Moderator: admin

Post Reply
Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

About Question enthuware.ocpjp.v7.2.1580 :

Post by Wisevolk »

I don't understand the answer, you ask what can be the class of an object 'x' that can access 'reason'.
Then you give an example for answer Outerworld saying : i.reason. But i is an Innerpeace Object not an Outerworld... am I wrong ?

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

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by admin »

Yes, InnerPeace object is not an Outerworld but every Outerworld object contains an InnerPeace object referenced through i. So the example code given in option 2 is the way you can access i.reason

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

vikyocajp71
Posts: 2
Joined: Tue Feb 24, 2015 12:32 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by vikyocajp71 »

But this can happen only inside class OuterWorld, if you have an instance X of type OuterWorld outside OuterWorld there is no way to access InnerPeace "reason" private String, not even through 'i'. So the question is not very clear in my opinion :-) maybe its intended to be confusing

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

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by admin »

You are right. It can only happen inside class OuterWorld. But no matter where it happens, the class of x can only be OuterWorld.
If you like our products and services, please help us by posting your review here.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by lenalena »

I don't understand the correct answer as well. The question asks - "What can be the class of an object 'x' that can access 'reason'?" Since i.reason is a valid call - then the class of x can ALSO be InnerPeace. So reason can be accessed through both - InnerPeace instance and OuterWorld instance that references it's InnerPeace instance... I don't understand why OuterWorld is the only option.

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

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by admin »

Yes, InnerPeace can also be the class of x. But that is not one of the options. Had it been an option, you should have selected both. Option 3 is incorrect because it says "Only InnerPeace", which is not correct because OuterPeace is also valid.
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by horst1a »

And why can a subclass of outerworld not access reason ? Does a subclass not inherit the inner class and its members ?

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

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by admin »

horst1a wrote:And why can a subclass of outerworld not access reason ? Does a subclass not inherit the inner class and its members ?
Can you post some code to show what you mean?
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by horst1a »

This is the my code:
class OuterWorld
{ public InnerPeace i = new InnerPeace();
class InnerPeace
{ String reason = "none"; } }

If i make the following:
class OuterWorldSub extends OuterWorld

can OuterWorldSub access reason ? as the inner class is merely a member

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

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by admin »

Very sorry but I am still not clear on what you are asking. The first part of the code that you've given is not same as what is given in the question and the second part of your code i.e. class OuterWorldSub extends OuterWorld is incomplete.

Please post complete, compilable code to show what exactly are you trying to do.
If you like our products and services, please help us by posting your review here.

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by shamran99 »

The question is confusing. Actually the private member can only be accessed inside the class. But the private member of inner class can also be accessed by its outer class only after creating the inner class object.

From that its very clear that the class of an object x that can access reason is always should be InnerPeace.
Yes, OuterWorld has a public member of InnerPeace. But this makes no sense. Even now the outer class is depend on that InnerPeace object to access the reason.

cmPushkin
Posts: 1
Joined: Wed Nov 01, 2017 7:47 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by cmPushkin »

The part "private member of inner class can also be accessed by its outer class only after creating the inner class object" clearly states that OuterWorld has access to 'reason'. It doesn't matter how.

This question is really confusing. x can be InnerPeace, but not ONLY as it states in third answer. it can be subclass of OuterWorld (if such subclass is inside OuterWorld class), but not ANY (outside OuterWorld class it has no access to 'reason'). It can't be any class in the same package or any class. So the only answer left is the second one.

gaurav.sjsu
Posts: 2
Joined: Sun Feb 11, 2018 11:48 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by gaurav.sjsu »

I think only InnerPeace is the RIGHT answer. Let me tell you why?
1. reason is a private member of "InnerPeace" class. Hence it can not be accessed using a dot (.) operator even though you have an InnerPeace as a 'public' member of OuterWorld class.
2. When you explained that you can have a void m() method inside InnerPeace that allows you to access 'reason', your access from OuterWorld object, effectively, is to void m() method. Now, m() method has access to reason, which in turn, is inside InnerPeace class. Therefore the only class of an object that can have control over private member 'reason' will be InnerPeace.

Answer "Only InnerPeace." should have been the right answer.

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

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by admin »

You need to read the explanation carefully. It says, you can have a method m() inside OuterWorld, not InnerPeace. Like this:

Code: Select all

class OuterWorld
{
  public InnerPeace i = new InnerPeace();
  private class InnerPeace
  {  
   private String reason = "none";
  }
  void m(){
    System.out.println(i.reason);
  }
  
}
If you like our products and services, please help us by posting your review here.

yuir12
Posts: 22
Joined: Thu Dec 09, 2021 11:23 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by yuir12 »

Hello.


Just wanted to ask why there is compilation error at //2?

Thanks

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

Re: About Question enthuware.ocpjp.v7.2.1580 :

Post by admin »

Are you sure you are trying out the code exactly as given?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 29 guests