package protected and compile error

Discussion in 'Java' started by gk, Nov 3, 2006.

  1. gk

    gk Guest

    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
     
    gk, Nov 3, 2006
    #1
    1. Advertising

  2. gk

    Chris Smith Guest

    gk <> wrote:
    > 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.

    --
    Chris Smith
     
    Chris Smith, Nov 3, 2006
    #2
    1. Advertising

  3. gk wrote:
    > 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-1?Q?Arne_Vajh=F8j?=, Nov 3, 2006
    #3
  4. Chris Smith wrote:
    > 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
     
    =?ISO-8859-15?Q?Arne_Vajh=F8j?=, Nov 3, 2006
    #4
  5. Arne Vajhøj wrote:
    > Chris Smith wrote:
    >> 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 ...


    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
     
    Thomas Hawtin, Nov 3, 2006
    #5
  6. gk

    pacman Guest

    RTFM.

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

    gk wrote:
    > 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
     
    pacman, Nov 3, 2006
    #6
  7. gk

    Piotr Kobzda Guest

    gk wrote:

    > 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
     
    Piotr Kobzda, Nov 4, 2006
    #7
  8. gk

    Daniel Pitts Guest

    gk wrote:
    > 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.
     
    Daniel Pitts, Nov 4, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    586
    Andreas Klemt
    Jul 5, 2003
  2. Nagaraj
    Replies:
    1
    Views:
    913
    Lionel B
    Mar 1, 2007
  3. Philipp
    Replies:
    15
    Views:
    579
  4. Replies:
    3
    Views:
    966
    Roedy Green
    Jan 28, 2008
  5. tom
    Replies:
    11
    Views:
    838
    Martin Gregorie
    Jan 20, 2010
Loading...

Share This Page