The Artima Developer Community
Sponsored Link

Java Answers Forum
Can't access protected members of base class

4 replies on 1 page. Most recent reply: Nov 9, 2003 6:02 PM by Tim Vernum

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Steve

Posts: 5
Nickname: srbeckle
Registered: Nov, 2003

Can't access protected members of base class Posted: Nov 9, 2003 10:53 AM
Reply to this message Reply
Advertisement
I was under the impression that a class B that extends class A has access to all the protected members/methods of class A. I'm getting a compile error when a class B in my default package tries to access a protected method of the base class A that it extends (class A exists in a different package). Any ideas on why this isn't working as advertised?


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: Can't access protected members of base class Posted: Nov 9, 2003 2:11 PM
Reply to this message Reply
A method in class B has access to protected members/methods of A, but only if it accesses them via a B object. Er, not sure if that's true for statics.

Try posting a small code snippet?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Can't access protected members of base class Posted: Nov 9, 2003 3:13 PM
Reply to this message Reply
> Try posting a small code snippet?

Or at the very least, provide a hint like the compiler error. You could get "a compiler error" for millions of reasons, most of which have nothing to do with access tags. For instance, if it is "error: cannot read: A.java" it is probably caused by whether a method is protected, but by the fact that the file doesn't exist.

Steve

Posts: 5
Nickname: srbeckle
Registered: Nov, 2003

Re: Can't access protected members of base class Posted: Nov 9, 2003 3:45 PM
Reply to this message Reply
OK, I got it working to some degree. I can do the following:

Class A {
static protected int i;
protected int j;
}

Class B extends A {
A.i = 10; //no compiler error
A myA = new A();
myA.j = 20; // no compiler error
}


What I don't understand is why I can't access i or j directly through an instantiation of B:

public Class Prog () {
public static void main( String[] args ) {
B myB = new B();
B.i = 100; // compiler disallows, says i is protected
B.j = 33; //disallowed..access is protected
}


....so, it appears that I have to create a public method in B that accesses i or j if I want to be able to alter them in Class Prog. It doesn't make sense to me that I can access A's protected data from within B's methods, but I can't access A's protected data by creating an instance of B, then using B.i or B.j in the class that created B.

Tim Vernum

Posts: 58
Nickname: tpv
Registered: Dec, 2002

Re: Can't access protected members of base class Posted: Nov 9, 2003 6:02 PM
Reply to this message Reply
> ....so, it appears that I have to create a public method
> in B that accesses i or j if I want to be able to alter
> them in Class Prog. It doesn't make sense to me that I can
> access A's protected data from within B's methods, but I
> can't access A's protected data by creating an instance of
> B, then using B.i or B.j in the class that created B.

That is the whole point of protected.

The behaviour you're seeing is the exact reason why protected exists.
In order to understand why having a feature like that is useful, you need to come to an understanding of "information hiding".

Assume we have class Foo

Let's start with a field declared as
public int x ;
. That says any piece of code in your whole program can access "x" if it has an instance of Foo to get at. So,
foo.x = 1 ;
is always valid code.


Now let's jump to the other end of the scale and talk about
private int z ;
. That says that only code within Foo can access "z". So,
foo.z = 1 ;
is only valid code inside Foo directly. No other class can have this code, or you'll get a compile error.


So, then we have
protected int y ;
. That says that "y" can be accessed from inside Foo, it can be accessed from inside any class the extends Foo, but not from anywhere else. So,
foo.y = 1 ;
is only valid code inside a class that is either Foo, or is extended from Foo. If a class does not extend Foo, then it is not allowed to get access to "y". Ever.

In your example you expected that you'd be able to access "i" on class "B" because "B" is derived from "A". But that's a misunderstanding of what the access controls mean. They aren't about controlling which objects can be accessed, they're about controlling which classes can perform the access.

So the fact that B is derived from A doesn't mean that Prog can now get at the protected data in B. Rather it means that now B can get at the protected data.

The short of it is: Whether you can access protected fields is determined by who you are relative to the object being accessed, not just by the type of the object being accessed..

Flat View: This topic has 4 replies on 1 page
Topic: package com.bruceeckel.simpletest does not exist Previous Topic   Next Topic Topic: how do i design/code for optional features?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use