• 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

A doubt about Protected Member

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,i m preparing for SCJP i m having one doubt that--->if superclass A having one protected member x and Class B in diff package extends this class A ..will the result is x is inherited to B but it x now become private to B...??? and if some other class C extends B....how it able to accesss x ???
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A protected variable is available to all subclasses (and subclasses of those classes) regardless of the package they are in. They don't 'become private' or anything like that.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Gray:
A protected variable is available to all subclasses (and subclasses of those classes) regardless of the package they are in. They don't 'become private' or anything like that.



False, sorta. They do become "private".

Here's the deal. String foo in A has no access modifier so any class in that package has access. String in A is changed to protected. Access expands to include immediate subclasses.

The emphasis is important. If B extends A it has access to String foo. If C extends B, String foo is private for all intents and purposes. C will not have access to String foo even.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick O'Shay:
The emphasis is important. If B extends A it has access to String foo. If C extends B, String foo is private for all intents and purposes. C will not have access to String foo even.



I could not believe that and I tried it out. Result: C can access foo. That is what I got from the language specification and that is what Sun's 1.4.2 compiler thinks.

Olaf
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick,

I don't know where you got this information. If you are sure about this prove it! I also did not believe you and tried it. C
can access the protected member of the A class.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C cannot access x because pretected member can only be seen by the subclass member which extend the class having x only. C cannot see x even it extends B.
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this using JDK 1.4.2.

A has a protected member x (in package a)
B extends A (in package b)
C extends B (in package c)


C can access x

Maybe it's in the specification that x should not be accessible by C but someone please prove it with a paragraph in the spec. I am 100% sure that I can access x in class C in JDK 1.4.2. If it is stated in the specification then it must be a bug.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I agree with Stuart Gray. The protected members don't become private or anything in Java..It will available to "all" subclasses regardless of the package they are in.

May be this "becoming private" term is influenced by C++ inheritance theory...But in Java its not that..

Thanks
Maulin
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, just be clear what is meant by accessing a superclass's protected member. The subclass can inherit it but cannot make an instance of the parent class and then access the member by using the dot operator. Here is some code in the subclass.

So, the inherited protected member can be read by or written to by the subclass - but that subclass cannot give anyone else access to it! Another class which has a reference to the subclass cannot use that reference to see it. This means that the superclass's protected member becomes effectively a private member in the subclass.

To reiterate: a protected member's access is restricted just to inheritance. If you understand this, you won't go far wrong.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All well...

I have a code snippet where protected variable of parent (Class A) is used by child (Class B) and child (Class B) of child (Class C).

class A {
protected static int a=88;
}

class B extends A{
protected static int retTest(){
a++;
return a;
}
}

class C extends B{
public static void main(String []mk){
System.out.println("In C::"+retTest());
System.out.println("In C:: 2::"+(--a));
}
}

The output is as follows:
In C::89
In C:: 2::88


That means class C is able to access the protected variables of class A.
 
Muks Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output is as follows:
In C::89
In C:: 2::88


That means class C is able to access the protected variables of class A by creating its instance too.
[ June 24, 2005: Message edited by: Muks Kumar ]
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course it can access that member variable. We are discussing protected variables in different packages. Your code snippet does not deal with that.
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clear this issue up once and for all read Controlling Access to Members of a Class from the java tutorial. Test out the code in this tutorial as it explains what goes on with protected access.
reply
    Bookmark Topic Watch Topic
  • New Topic