The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
SCJP 5
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Originally posted by Stevi Deter:
B and TestClass are not in the same package as Object, so they cannot see the protected java.lang.Object.clone() member that is inherited by A through extending Object.
SCJP 5 & 6, SCWCD 5, SCBCD 5
Thanks<br />Dinesh
Originally posted by Dinesh Tahiliani:
System.out.println(b1==b2);
o/p --
TestClass@360be0
TestClass@45a877
TestClass@1372a1a
false
its giving me false, even though i implemented the Clone
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Thanks<br />Dinesh
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Thanks<br />Dinesh
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Originally posted by Francesco Bianchi:
In your hierarchy, A comes right after Object. All the subsequent subclasses have to pass through A to reach Object members. And as long as both A, B and TestClass are in a different package from Object, A is THE ONLY class allowed to SEE protected Object members. But still A doesn't have a clone() method. It can just use it.
Originally posted by Ismael Upright:
If class X has a protected method doSomething() and we subclass this class with A, the method doSomething() is inherited and exists in class A. It can be used with instance a of class A as follows: "a.doSomething()". Right?
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |