Page 1 of 1
About Question enthuware.ocajp.i.v7.2.981 :
Posted: Tue Jul 24, 2012 3:30 am
by ETS User
Your answer is totally incorrect on object o1 of class A member of class B can be available only thorough inheritance not using dot.
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Tue Jul 24, 2012 4:10 am
by Guest
Code: Select all
class B{
int member=10;
}
class A
{
//1
A o1=new A();
System.out.println(o1.member); //won't compile
//not possible without inheritence even in same pkg
//2
B o2=new B();
System.out.println(o2.member); //compiles
}
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Tue Jul 24, 2012 8:10 pm
by admin
Hello,
The given question and answer are both correct. The question statements clearly specifies that o1 and o2 are different objects. o1 refers to an object of class A and o2 refers to an object of class B. Further, it clearly says you want to access a member field of o2 from o1, which means o2.member (the way you are doing at //2 in your example.)
In no way can the given statement be interpreted as in //1 of your example.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Sun Mar 10, 2013 12:08 pm
by The_Nick
Hi,
I agree with the posts above, this question really appears to ask something different from what you are aiming to ask.
Take it as my personal feedback for this only question, the rest of the bank question is fantastic.
The_Nick.
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Fri Mar 15, 2013 5:32 pm
by muttley
I didn't understand your explanation.
Could you make some example?
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Sun Jan 26, 2014 12:59 pm
by ml.vilen
this question is incorrect for sure.
from the question can be inferred that o1 need to access something like o2.member that leads to some relationship of classes A and B such that o2.member=o1.member and this can be achieved only via using inheritance.
p.s. if I will find 2 more mistakes you gotta refund me

Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Sun Jan 26, 2014 9:24 pm
by admin
The question and answer are correct as explained above. Accessing a member of another object doesn't imply inheritance. Inheriting a member of another class implies inheritance.
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Thu Jan 15, 2015 10:56 am
by jmrego
This question is quite confusing. It says "For object o1 of class A" and not "from class A". So my first assumption was this:
Code: Select all
class A { }
class B { int x = 0; }
public class TestClass {
public static void main(String args[]) {
A a = new A();
B b = new B();
System.out.println(a.x);
}
}
So, the only way for this to work is if "B is a Super class of A".
Accessing from an object is not the same as accessing from a class, I suppose.
In this case:
Code: Select all
class B{ int member=10; }
class A {
B o2=new B();
System.out.println(o2.member); //compiles
}
you are accessing from "class A" to an "object member of class B" and not from an "object of class A" to an "object member of class B". Right?
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Thu Jan 15, 2015 9:50 pm
by admin
I give in.
Will change it

Paul.
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Sat Jan 02, 2016 8:57 am
by EfimGraur
hello,
So the correct answer is " in the same package and must be a Subclass of A. " ?
Thank You,
Efim
Re: About Question enthuware.ocajp.i.v7.2.981 :
Posted: Sat Jan 02, 2016 9:29 pm
by admin
The problem statement has been updated to :
"For class A to access a member(field or method) of class B, when the member has no access modifier, class B must be...".
Now, only option 2 is correct.
thank you for your feedback!
Paul.