All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
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
}
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.
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.
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
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.
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.
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.