Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

package protected and compile error

Thread view: 
gk - 03 Nov 2006 18:24 GMT
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
Chris Smith - 03 Nov 2006 18:47 GMT
>                 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
[quoted text clipped - 3 lines]
>
> 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.

Signature

Chris Smith

Arne Vajhøj - 03 Nov 2006 19:09 GMT
> 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
Thomas Hawtin - 03 Nov 2006 22:14 GMT
>> Second of all, the problem is that you are misunderstanding the
>> protected modifier.  It allows access to a method or field in the
[quoted text clipped - 4 lines]
>
> 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
Arne Vajhøj - 03 Nov 2006 19:08 GMT
> package packageY;
>
[quoted text clipped - 3 lines]
>         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
pacman - 03 Nov 2006 23:34 GMT
RTFM.

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

> code-1
> -------------
[quoted text clipped - 37 lines]
>
> thank you
Piotr Kobzda - 04 Nov 2006 01:42 GMT
>                 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
Daniel Pitts - 04 Nov 2006 01:53 GMT
> code-1
> -------------
[quoted text clipped - 37 lines]
>
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.