• 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt Regarding Protected Access

Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does a protected member accessible in a subclass which is a subclass outside package that inherits a protected member.

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.

If the above statement is true then why is a private memeber getting inherited in the subclass of a subclass outside the package.

package com.one
class A
protected int i=10;

package com.two
class B extends A
//i is accessible only through inheritence

package com.two
class C extends B

// i is not accessible through Reference ok..
//i is still accessible through inheritence???
//why is this so if i becomes private in B

Help me out!
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rav,

Here the catch is,
1)Any class in the package com.one, can access protected memeber through
instance creation like ... A a = new A(); a.i

2)But coming to package com.two, No class is allowed to access i through
instance. Its only private to subclasses in the sense like the relation
ship between Parent and Child. Every child will see it as protected only.
the access modifier won't change.

Hope you got it.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic