About Question enthuware.ocpjp.v7.2.1580 :
Moderator: admin
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1580 :
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 ?
Then you give an example for answer Outerworld saying : i.reason. But i is an Innerpeace Object not an Outerworld... am I wrong ?
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Feb 24, 2015 12:32 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
-
- Posts: 56
- Joined: Tue Feb 21, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
And why can a subclass of outerworld not access reason ? Does a subclass not inherit the inner class and its members ?
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
Can you post some code to show what you mean?horst1a wrote:And why can a subclass of outerworld not access reason ? Does a subclass not inherit the inner class and its members ?
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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
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
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
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.
-
- Posts: 15
- Joined: Wed May 10, 2017 2:49 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
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.
-
- Posts: 1
- Joined: Wed Nov 01, 2017 7:47 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
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.
-
- Posts: 2
- Joined: Sun Feb 11, 2018 11:48 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
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.
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
-
- Posts: 22
- Joined: Thu Dec 09, 2021 11:23 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
Hello.
Just wanted to ask why there is compilation error at //2?
Thanks
Just wanted to ask why there is compilation error at //2?
Thanks
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1580 :
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.
Who is online
Users browsing this forum: No registered users and 40 guests