About Question enthuware.ocpjp.v7.2.1355 :

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

Moderator: admin

Post Reply
riverbox
Posts: 5
Joined: Wed Mar 27, 2013 4:14 am
Contact:

About Question enthuware.ocpjp.v7.2.1355 :

Post by riverbox »

Object class defines a clone() method, but this method is protected. Therefore, you cannot call B's clone() from outside class B.
Why? If metod is protected then descendants class can use this metod. Right?

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

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

Post by admin »

Yes, you can call B's clone method from inside class B but you cannot call B's clone method from C (for example). That is what is meant by "from outside class B".
If you like our products and services, please help us by posting your review here.

Student
Posts: 53
Joined: Fri Sep 20, 2013 7:20 am
Contact:

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

Post by Student »

My understanding is you can call a protected method (in a public class) from any class in the same package but outside the package you must subclass the class and use a reference of the subclass type to invoke the method.

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

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

Post by admin »

That is correct but in this case the protected method is in Object class, which is in java.lang package.

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

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

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

Post by Ambiorix »

admin wrote:That is correct but in this case the protected method is in Object class, which is in java.lang package.

HTH,
Paul.
While a quick test run proves that you're correct, I don't follow the logic. TestClass is a subclass of Object, so why can't it call Object's protected methods?

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

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

Post by admin »

You can indeed call clone on a TestClass object from within TestClass. You cannot call B's clone() from TestClass.

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

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

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

Post by Ambiorix »

admin wrote:You can indeed call clone on a TestClass object from within TestClass. You cannot call B's clone() from TestClass.

HTH,
Paul.
Why not? TestClass is in the same package as B and the access level is protected.

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

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

Post by admin »

As I explained above, B does not define clone. B inherits clone that is defined in Object class, which is in a different package. This is how the language has been designed to work.

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

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

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

Post by Ambiorix »

That takes me back to my original question.

If B inherits clone that is defined in the Object class, and TestClass is a subclass of Object, why can't TestClass call B's clone?

I'm really struggling to grasp what's going on here.

Either the access is governed by the relationship between Object and TestClass (TC is a subclass of Object so it should work)

OR

the access is governed by the relationship between B and TestClass (they're in the same package so it should work)

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

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

Post by admin »

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

Velocidrak
Posts: 3
Joined: Sun May 19, 2013 5:45 pm
Contact:

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

Post by Velocidrak »

Ambiorix,

The "protected" access modifier only allows blanket access to the top level package, and individual access to subclasses themselves, but not the package that the subclass is in(if it is different from the top level class).

Although it is true that TestClass extends Object implicitly and can call it's own clone method, since it is not in the same inheritance tree as B it does not have permission to call B's clone method. In order for TestClass to have permission to call B's clone method, it would need to extend class B.

tolkat
Posts: 1
Joined: Fri Sep 20, 2013 2:52 am
Contact:

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

Post by tolkat »

It's because 'Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass.'

Elmcrest
Posts: 12
Joined: Sat Apr 20, 2013 10:06 am
Contact:

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

Post by Elmcrest »

Regarding protected: Also note that when the sub-class overrides the protected method of the super-class, THEN all classes of the package of the sub-class can access this method since it is redefined in the sub-class.
Just wanted to mention it since I often confuse this one..

Kind regards.

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

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

Post by admin »

Elmcrest wrote:Regarding protected: Also note that when the sub-class overrides the protected method of the super-class, THEN all classes of the package of the sub-class can access this method since it is redefined in the sub-class.
Just wanted to mention it since I often confuse this one..

Kind regards.
You have to be careful with that because access checking is based on the declared class of the reference. So, for example, if you have A a = new B(); [where B extends A] and if A has a protected method m that is overridden by B, you still can't do a.m(); from B's package if A is in a different package.

-Paul.
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.1355 :

Post by jagoneye »

Ambiorix wrote:That takes me back to my original question.

If B inherits clone that is defined in the Object class, and TestClass is a subclass of Object, why can't TestClass call B's clone?

I'm really struggling to grasp what's going on here.

Either the access is governed by the relationship between Object and TestClass (TC is a subclass of Object so it should work)

OR

the access is governed by the relationship between B and TestClass (they're in the same package so it should work)
You should first understand how protected works. Try running some code with access modifiers and inheritance. To save your time, protected means you can access in the same package or subclass(which may be in another package).
But if it is in another package you can't directly access the members of superclass without inheriting it.
Suppose you have a class Animal and it has protected attributes like hands, legs.
Now you can't directly access those attributes by new Animal().hands/legs
if you are in a different package. That could be possible had it been public.
So what you need to do is first extend Animal (assuming you are in different package)
say Dog extends Animal (which means it inherits hands, legs) and now note
you can access those by new Dog().hands/legs.
The use of protected is rare but mainly it is used to restrict the access of instance members to their immediate subclasses which may be in a different package.
And if new Animal().legs/hands would be possible then there would be no difference between public modifier and protected modifier. A practical example would be suppose your application needs some properties or attributes from another application designed by you in another package, then you can declare those properties or attributes protected and can use them in your other application. This adds security to your application because only you know the implementations of your class, it would be hard for someone else to access those properties without knowledge of the implementation(if it had been public then anyone could access those attributes without your permission, so now you know how protected works!). Hope this long explanation cleared your mind.
Cheers, Salman :)

Post Reply

Who is online

Users browsing this forum: No registered users and 97 guests