About Question enthuware.ocajp.i.v7.2.981 :

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.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.981 :

Post by ETS User »

Image

Your answer is totally incorrect on object o1 of class A member of class B can be available only thorough inheritance not using dot.

Guest

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post 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
}

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

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post 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.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post 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.

muttley
Posts: 19
Joined: Thu Feb 28, 2013 9:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post by muttley »

I didn't understand your explanation.
Could you make some example?

ml.vilen
Posts: 4
Joined: Mon Jan 13, 2014 11:01 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post 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 :)

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

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post 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.

jmrego
Posts: 8
Joined: Wed Dec 10, 2014 6:05 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post 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?

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

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post by admin »

I give in.
Will change it :)
Paul.

EfimGraur
Posts: 4
Joined: Fri Oct 16, 2015 1:25 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post by EfimGraur »

hello,

So the correct answer is " in the same package and must be a Subclass of A. " ?

Thank You,
Efim

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

Re: About Question enthuware.ocajp.i.v7.2.981 :

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests