• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Protected Modifier Question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I have a question regarding the proctected modifier..
--------------

package packed;
public class pack
{
static public int x1 = 7;
protected int x2 = 8;
static int x3=9;
static private int x4 =10;
}
----

import packed.pack;
class test3 extends pack
{
public static void main( String args[] )
{
pack p = new pack();
System.out.println(p.x2);
}


}

I got an error saying that x2 is not visible on line "System.out.println(p.x2);" of test3 class.

I thought the protected modifier on variables would mean that subclasses and classes in the same package would have access to the protected variable of the parent class?

Thanks

Stephen
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Protected member can be accessed only using inheritence cannot be accessed through superclass reference.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What gaurav told is right.

Protected member can be accessed only using inheritence cannot be accessed through superclass reference.



This means that the following method if kept in test3 will compile.

public void method( String dgd )//can be any signature
{
x2 = x2 + 10;//will compile
System.out.println(x2);//will compile
//pack p = new pack();
//System.out.println(p.x2); //will not compile
}



I think now, you got the point.

Let me know, if I am wrong.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Class "test3" has access only to the inherited from "pack" protected elements, i.e., its own elements, but the proetected data of OTHER "pack" instances is not accessible from "test3".
[ June 29, 2006: Message edited by: wise owen ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the Java Language Specification says about protected access.

Details on protected Access
reply
    Bookmark Topic Watch Topic
  • New Topic