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.
About Question enthuware.ocpjp.v7.2.1580 :
Moderator: admin
-
- Posts: 15
- Joined: Wed May 10, 2017 2:49 am
- Contact:
-
- 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: 10065
- 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: 10065
- 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: Bing [Bot] and 101 guests