package protected and compile error

G

gk

code-1
-------------

package packageX;

public class SuperclassX {
protected void superclassMethodX() {
}

protected int superclassVarX;
}


code-2:
------------
package packageY;

import packageX.*;
public class SubclassY extends SuperclassX
{
SuperclassX objX = new SubclassY();
SubclassY objY = new SubclassY();
void subclassMethodY()
{
objY.superclassMethodX();
int i;
i = objX.superclassVarX; // compile error is here
}

}


Not understanding why there is compile error ?
objX is a type (reference to the Subclass object ) and compiler is
always happy to check with the type .....the objX type really indeed
have superclassVarX as a protected member ....and so there should
not have been any compile error.

I am getting a compile error which is not understandable.

please explain .

thank you
 
C

Chris Smith

gk said:
i = objX.superclassVarX; // compile error is here
Not understanding why there is compile error ?
objX is a type (reference to the Subclass object ) and compiler is
always happy to check with the type .....the objX type really indeed
have superclassVarX as a protected member ....and so there should
not have been any compile error.

I am getting a compile error which is not understandable.

First of all, just because you don't understand the compiler's error
message, don't assume that no one else can. Why did you make it harder
to answer your question by refusing to tell us what the error message
is?

Second of all, the problem is that you are misunderstanding the
protected modifier. It allows access to a method or field in the *same*
object, in code that belongs to a subclass. It does NOT allow you to
access members of other objects. If you want that, you need to make the
member public, or move this code into the same package.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

gk said:
package packageY;

import packageX.*;
public class SubclassY extends SuperclassX
{
SuperclassX objX = new SubclassY();
SubclassY objY = new SubclassY();

I do not know what the compile error is.

I suggest you give us the exact error.

But the line above should give a runtime error
(stack overflow).

Arne
 
?

=?ISO-8859-15?Q?Arne_Vajh=F8j?=

Chris said:
Second of all, the problem is that you are misunderstanding the
protected modifier. It allows access to a method or field in the *same*
object, in code that belongs to a subclass. It does NOT allow you to
access members of other objects.

Really ?

My java compiler has a bug then ...

Arne
 
T

Thomas Hawtin

Arne said:
Really ?

My java compiler has a bug then ...

Mine too. I want my money back! :)

You can access protected variables through references which are
assignment compatible with the class that the code is in.

So if you have a base class, Base, with a protected field, a subclass
Derived would be able to access the field for a variable of type Derived
(or a subclass of Derived) but not from a variable of type Base.
(Assumes Base and Derived are in different packages.)

Just don't ask me about protected and inner classes. It's a bit nasty,
and it has changed from the original spec. I suggest limiting the use of
protected, and prefer delegation over inheritance.

Tom Hawtin
 
P

Piotr Kobzda

gk said:
i = objX.superclassVarX; // compile error is here

You can do that this way:

i = ((SubclassY)objX).superclassVarX;

Of course objX must be instance of SubclassY here.
Not understanding why there is compile error ?

In addition to already posted advices reading JLS3 chapter 6.6.2.1 may
appear helpful too.


Anyway, if you really need that, you can quite easily extend the default
base class members access by introducing special static protected
methods in your base class.

That is, after adding the following methods to your SuperclassX:

static protected int getSuperclassVarXOf(SuperclassX me) {
return me.superclassVarX;
}
static protected void setSuperclassVarXOf(SuperclassX me, int val) {
me.superclassVarX = val;
}

you will be able to safely access/modify protected superclassVarX
instance field in all subclasses of your class, in the way like that:

i = getSuperclassVarXOf(objX);



piotr
 
D

Daniel Pitts

gk said:
code-1
-------------

package packageX;

public class SuperclassX {
protected void superclassMethodX() {
}

protected int superclassVarX;
}


code-2:
------------
package packageY;

import packageX.*;
public class SubclassY extends SuperclassX
{
SuperclassX objX = new SubclassY();
SubclassY objY = new SubclassY();
void subclassMethodY()
{
objY.superclassMethodX();
int i;
i = objX.superclassVarX; // compile error is here
}

}


Not understanding why there is compile error ?
objX is a type (reference to the Subclass object ) and compiler is
always happy to check with the type .....the objX type really indeed
have superclassVarX as a protected member ....and so there should
not have been any compile error.

I am getting a compile error which is not understandable.

please explain .

thank you

Think of it this way.

You have classX, classY extends classX, and classZ extends classX

classX x = new classX();
classX y = new classY();
classX z = new classZ();

should classY be able to access an object of type classZ's protected
data? I don't think so.
In order for classY to access the protected member of another instance,
that other instance has to be classY. This also has to be visible to
the compiler (either through a cast, or by having the reference
declared as type classY), otherwise the compiler doesn't know if you're
accessing a protected member of a class you are descendant from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top