Page 1 of 1
About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 9:30 am
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.
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 9:48 am
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.
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 9:59 am
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.
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 10:52 am
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.
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 2:37 pm
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.
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 2:40 pm
by admin
Ok, I see your point now. Thanks for clearing. Will update the question to make it more clear.
-Paul.
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 2:42 pm
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".
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 2:46 pm
by admin
"access" does not mean "inherit". It just means access i.e. b.someField or b.method();
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 3:08 pm
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.
Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Sat Sep 07, 2013 4:04 pm
by admin
OK, got it

Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Wed Jan 04, 2017 2:06 am
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.

Re: About Question enthuware.ocpjp.v7.2.1234 :
Posted: Mon Feb 13, 2017 6:22 am
by antoniosarco
More about.....
Java AccessModifiers
Anto