This discussion is archived
3 Replies Latest reply: Apr 1, 2013 5:07 PM by rp0428 RSS

Accessing private field of Derived object in Base class

956015 Newbie
Currently Being Moderated
Hi,

I have this piece of code I wrote a while ago to test something. The issue is accessing a private field of Base class in Base but of a Derived object.

Here is the code:
class Base
{
     private int x;
     
     public int getX()
     {
          return x;
     }
     
     public int getX(Derived d)
     {
          // return d.x; 
          return ((Base) d).x;
     }
}
The commented code does not work but casting d to Base does.
Can someone please explain the reasoning for this.

Forgot to mention that the compilation error is that x has private access in Base.

Thank you.

Edited by: 953012 on Apr 1, 2013 8:42 AM
  • 1. Re: Accessing private field of Derived object in Base class
    rp0428 Guru
    Currently Being Moderated
    >
    The issue is accessing a private field of Base class in Base but of a Derived object.
    . . .The commented code does not work but casting d to Base does.
    Can someone please explain the reasoning for this.
    >
    Sure - and that 'someone' is The Java Language Specification.
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#d5e8146
    >
    Example 6.6-5. Access to private Fields, Methods, and Constructors

    A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses. In the example:

    class Point {
    Point() { setMasterID(); }
    int x, y;
    private int ID;
    private static int masterID = 0;
    private void setMasterID() { ID = masterID++; }
    }
    the private members ID, masterID, and setMasterID may be used only within the body of class Point. They may not be accessed by qualified names, field access expressions, or method invocation expressions outside the body of the declaration of Point.

    See §8.8.8 for an example that uses a private constructor.
  • 2. Re: Accessing private field of Derived object in Base class
    956015 Newbie
    Currently Being Moderated
    Hi RP,

    Thanks for the reply.
    I don't quite understand the explanation however.

    As I understand the explanation says that you can access any private member within the code of the class that encloses the private member. So in this case x is the private member and the line of code (return d.x) is in Base which encloses the private member. Does it have to do with the fact that the Derived class does not in fact inherit the private members of Base?

    But considering this code:
    Base
    {
         private int x;
         
         public int getX()
         {
              return myX();
         }
         
         private int myX()
         {
              return this.x;
         }
    }
    If outside Base code I have Derived d = new Derived() and I call d.getX() then isn't that like calling d.x in myX()?

    Thank you for any help.
  • 3. Re: Accessing private field of Derived object in Base class
    rp0428 Guru
    Currently Being Moderated
    >
    As I understand the explanation says that you can access any private member within the code of the class that encloses the private member. So in this case x is the private member and the line of code (return d.x) is in Base which encloses the private member. Does it have to do with the fact that the Derived class does not in fact inherit the private members of Base?
    >
    It has to do with the entire quote from the spec
    >
    A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses
    >
    Your code is
    public int getX(Derived d)
         {
              // return d.x; 
              return ((Base) d).x;
         }
    The 'Derived' class is NOT 'the top level class that encloses the declaration of the member'. It does NOT inherit 'x' which is a private member of 'Base'. As far as the 'Derived' class is concerned 'x' does not exist.
    >
    If outside Base code I have Derived d = new Derived() and I call d.getX() then isn't that like calling d.x in myX()?
    >
    How is that the same? 'Base' owns 'x' and can do whatever it wants with it. 'Derived' has no knowledge of 'x' and CAN NOT access it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points