• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

protected variables

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To say protected variables are visible from a subclass outside the same package.

Does this mean if I have a public class that has a protected member and I have another class in a different package and this class is a subclass of the public class that has a member I can access it directly right?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package one;
public Class A{
protected int test = 10;
}
public Class B extends A{
}
}

package two;
public Class C{
public static void main (String args){
B myObj = new B();

// I get field is not visible I thought this is okay
System.out.println(myObj.test);
}
}
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to import ClassB for it to be visible.

You still will not be able to modify the protected member even if you import it though.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I just typed this without compiling it. But if I do import it why cant I access the member but if I have the class in the same package I can.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Francis,
It is not possible to access the protected variables and methods by reference in a class which is outside the package. However, they can be accessed only through inheritance.
In your example, you are creating myObj which is a reference to B and using this reference you are trying to access protected member variable test which is not allowed.
[ November 09, 2004: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few observations:

1. You can't have two public classes in the same file, which appears to be the case in your code.

2. If A is in one package and has a protected member, and B is in another package, then the protected member is visible to B only. On inheritance of a protected member from another package, that member becomes as though it were private in the subclass. Therefore, any class attempting to access the (now-private) member of the subclass will not be able to "see" the member.

Does that help clarify?
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another observation:

If your class C extends A, your code will work. Because C does not extend B or A, it cannot see the protected member of A through B because permitting it to work this way would remove the protection and defeat the purpose of the protected access modifier.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jay, so basicly protected members cannot be accessed DIRECTLY outside of the package but CAN be if its in the same package?

Jeff, I was typing this quickly and I did'nt realize I put the same class in the same file but I meant them to be in a separate file. You said if my class C extends A my code will work. I disagree if my class C is in a different package than A and subclassing A my code will still not work based on what Jay said that I cant access the members directly outside of the package. But if they were in the same package I can access them directly.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Francis,
Same package should work... see the code below


[ November 09, 2004: Message edited by: Jay Pawar ]
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I disagree if my class C is in a different package than A and subclassing A my code will still not work based on what Jay said that I cant access the members directly outside of the package. But if they were in the same package I can access them directly.



Not true. If C is in a different package than A, and C directly subclasses A, then C can access A's protected members.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C can access the protected members through inheritance but I was asking about if I directly access the protected member by using a reference of C.

C a = new C();

// will not work because in a diff package but if the same package its okay
System.out.println(a.test);
[ November 09, 2004: Message edited by: Francis Palattao ]
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah! I misunderstood the question. I stand corrected, though I'm actually sitting down...
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic