| Author |
confusion regarding protected keyword
|
Rohan Pujari
Greenhorn
Joined: Feb 27, 2007
Posts: 24
|
|
I've read in the Kathy sierra book that when a subclass outside outside the package inherits the protected data member of the super class then the inherited member becomes private in the subclass so that no other class can access it using either inheritance or reference.I've written some code which contradicts the same.please go through the code and lemme know if its true or maybe I've gone wrong somewhere. THIS IS THE SUPERCLASS IN ROHAN PACKAGE package rohan ; public class SuperClass { protected int x ; } THIS IS SUBCLASS OUTSIDE THE ROHAN PACKAGE IE DEFAULT PACKAGE import rohan.SuperClass ; class SubClass extends SuperClass { public static void main(String[] args) { SubClass sc = new SubClass() ; System.out.println(sc.x) ; } } now as per the book the variable x has become private in SubClass and so is only accessible in subclass but look at the following code. SimpleClass is class in default package but should not know anything about x since it is private class SimpleClass extends SubClass { public static void main(String[] args) { SimpleClass sc = new SimpleClass() ; System.out.println(sc.x) ; } } but this code produces output 0 as if the member is still protected
|
 |
Shiva Shankar
Ranch Hand
Joined: Dec 07, 2006
Posts: 31
|
|
|
As K&B puts it "Once the subclass outside the package inherits the protected member, that member becomes private to any code outside the subclass, with the exception of subclasses of the subclass"
|
 |
Peter Schubert
Greenhorn
Joined: Sep 15, 2006
Posts: 5
|
|
Also see this thread. [ February 28, 2007: Message edited by: Peter Schubert ]
|
 |
Rohan Pujari
Greenhorn
Joined: Feb 27, 2007
Posts: 24
|
|
hey its not given this way in the book.this is straight from the book. "No! 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. So if class Neighbor instantiates a Child object, then even if class Neighbor is in the same package as class Child, class Neighbor won�t have access to the Child�s inherited (but protected) variable x. The bottom line: when a subclass-outside-the-package inherits a protected member, the member is essentially private inside the subclass, such that only the subclass� own code can access it."
|
 |
Thuwaragan Sundaramoorthy
Greenhorn
Joined: Jan 11, 2007
Posts: 26
|
|
"No! 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. So if class Neighbor instantiates a Child object, then even if class Neighbor is in the same package as class Child, class Neighbor won�t have access to the Child�s inherited (but protected) variable x. The bottom line: when a subclass-outside-the-package inherits a protected member, the member is essentially private inside the subclass, such that only the subclass� own code can access it."
It should end as .....such that only the subclass and its subclasses can access it.
|
------------------------------<br />SCJP 1.5
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
You may refer to my post at: Move to my posting to clarify Protected to clarify protected!
|
cmbhatt
|
 |
 |
|
|
subject: confusion regarding protected keyword
|
|
|