• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confusion regarding protected keyword

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see this thread.
[ February 28, 2007: Message edited by: Peter Schubert ]
 
Rohan Pujari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may refer to my post at:

Move to my posting to clarify Protected
to clarify protected!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic