About Question enthuware.ocpjp.v7.2.1234 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

About Question enthuware.ocpjp.v7.2.1234 :

Post by The_Nick »

Hi everybody,
the explanation of the question is right:
No access modifier means "default" access which means only classes of the same package can access it.
Note that there is no 'default' access specifier. Putting no keyword is default access.
However in the question you are talking about object not class:
For object o1 of class A to access a member(field or method) of object o2 of class B, when the member has no access modifier, class B must be...
Therefore the right answer should be "A subclass of A", isn't it?

The_Nick.

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

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by admin »

The fields are defined in a class but it is an object of that class that the field really exists in at runtime. A Class is just a definition. At run time it is the objects that interact (based on how their classes have been defined).

So the given answer and explanation are correct.
If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by The_Nick »

From how the question is put it appears as if you mean that the following is legal:

Code: Select all

public class A {
public static void main (String []args)
{
	A Object1 = new A();
	Object1.field;
	
}
class B
{
	int field = 4;
	
}
}
Instead you mean in the declaration of the object (in the class). As far as I know an object is an object when new Object() is called.
However now I understood you mean the class by an object in that context, however I would like to point out that could be misleading.

The_Nick.

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

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by admin »

The_Nick wrote:As far as I know an object is an object when new Object() is called.
No, that would be an Object (with capital O). An object (with a small o) is an object when you do a new on any class (not just Object class).

The question clearly says, "an object of class A", which means you are doing new A() (or new SubclassOfA() ). There is no ambiguity here.


HTH,
Paul.
If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by The_Nick »

admin wrote:
The_Nick wrote:As far as I know an object is an object when new Object() is called.
No, that would be an Object (with capital O). An object (with a small o) is an object when you do a new on any class (not just Object class).

The question clearly says, "an object of class A", which means you are doing new A() (or new SubclassOfA() ). There is no ambiguity here.


HTH,
Paul.
The ambiguity I was talking about is not referred to Object or object but object and class.
For an object of class A to access the field or method of an object of class B, object A must be a subtype of B.
That's what I thought when I read the question. I thought that you meant "object" as the final product of the class ie: A object = new A();.
When you replied I see that you are refering at the Class more than the object itself (new A()). and everything then was clear.
Personally I think it would be clearer to replace object A and object B with class A and class B.
It's just my feedback to the question though, maybe it's me that I am getting wrong the meaning.
For the rest I love the software, I can only congratulate with all of you for the quality you provide.
Thanks.

The_Nick.

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

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by admin »

Ok, I see your point now. Thanks for clearing. Will update the question to make it more clear.

-Paul.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by admin »

The_Nick wrote: For an object of class A to access the field or method of an object of class B, object A must be a subtype of B.

The_Nick.
This is not correct though. i.e. it is not a must that object A be a subtype of B. The field may be public or both the classes can be in same package and the field may be "default".
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by admin »

"access" does not mean "inherit". It just means access i.e. b.someField or b.method();
If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by The_Nick »

admin wrote:"access" does not mean "inherit". It just means access i.e. b.someField or b.method();
I meant in case the question was referring to real objects.

if there is no inheritance between them even though the instance variables are declared "default" for object A (not being subtype of B) is not possible to access fields of class B.

Code: Select all

class A { // if A would extend B it would compile
int ei=0;

}
public class B{
int bi=0;
public static void main (String[] args)
{
A object = new A();
System.out.println(object.bi); // compile error;
}
}
Basically this is what I meant.

This is instead what you mean:

Code: Select all

class A {
int ei=0;
int bi = new B().bi; // yes I have access to B however I still need to create an object and make an has-a relationship
}
public class B{
int bi=0;
public static void main (String[] args)
{
A object = new A();

System.out.println(object.bi); // compile error;
}
}
 
That's all, and again is just my point of view.

The_Nick.

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

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by admin »

OK, got it :)
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by jagoneye »

default cannot be used as an access modifier because 'default' is a java reserved keyword for switch-case where you have a default case and hence it cannot be used anywhere else. :)

antoniosarco
Posts: 2
Joined: Mon Feb 13, 2017 6:20 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1234 :

Post by antoniosarco »

More about.....Java AccessModifiers

Anto

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests