This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private variables in superclass accessed by subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private variables in superclass accessed by subclass" Watch "Private variables in superclass accessed by subclass" New topic
Author

Private variables in superclass accessed by subclass

Selim Hendrickson
Greenhorn

Joined: Aug 14, 2003
Posts: 9
Hello, the below code seems to access the private variable x. How can that be possible? Thinking that private variables are not inherited by the class C, how can c.x() access the superclass private variable?
abstract class A {
private int x = 4;
private int y = 2;
public int x() {return x;}
public void x(int x) {this.x = x;}
public int y() {return y;}
public void y(int y) {this.y = y;}
}
public class C extends A{
public static void main(String[] args) {
C c = new C();
System.out.println(c.x());
}
}
The result is the output of 4.
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
What I have understood by the term "Accessing a variable" is refering to the variable directly.
Surely a public method can be used outside the class, even though inside the class it might be accessing the private variable.
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi:
This is a form of encapsulation. It is the most appropriate way for creating instance variable. The variables decleared private, and methods are created to set and access them. In your exampls, you are not accessing the variable directly, you only accessing a public method, which return the private variable. Like what I said, it is legal and the most appropriate way.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Selim Hendrickson
Greenhorn

Joined: Aug 14, 2003
Posts: 9
Hi, I think I haven't made myself clear. Now, if there were a superclass instance ( which is not possible since A is abstract ) such as:
A a = new A();
and an instance of class C had access to private variables of "a" through accessor method x() then I wouldn't have been confused.
But first of all there is no instance of A. There is only one object of class type C which extends and inherits all members of abstract A except for the private variables. Then x() method is invoked using an instance of class type c which I believe should be invoking the inherited x() method of c. But how does the inherited method x() return x when x is not inherited. x() simply executes as if it still has a connection with A. Obviously I must be missing something, so I'd be gratefull if you could point the flaw in my reasoning. I'm tired I need to
Thanks ahead...
[ September 03, 2003: Message edited by: Selim Hendrickson ]
[ September 03, 2003: Message edited by: Selim Hendrickson ]
Larry Mathys
Greenhorn

Joined: Sep 02, 2003
Posts: 7
I'll see if I can make this a little more clear. Just because a class is declared abstract, doesn't mean that the accessible methods and fields do not exist. (Which I noticed class A doesn't even need the abstract class modifier because none of the methods are declared abstract) Just add a constructor to the A object with a System.out.println("BLAH BLAH constructor A") statement and you'll see that the compiler does indeed create an object A. Abstract classes follow the same inheritance rules as any object. You declare an abstract class because you may want a base class as a starting point for other, more meaningful objects. For example, take a class Automobile, which defines certain criteria that every automobile has but is too 'abstract' to justify creating an object. How would you initialize it's state?? What color, horsepower, type??? As a programmer, you would want anyone using this abstract object to define subclasses based on the criteria you define as abstract methods in order to require a subclass to define it's behavior more concretely.
With that said, when the x() method is invoked on the C object, through inheritance rules C also contains an x() method THAT RETURNS BY VALUE an integer encapsulated in the A object. The C object has no access to the A.x instance variable, only to the value that x is when the x() method is called. Having access to that variable would mean anyone extending the A object could modify the variable. Which you obviously cannot do.
Hope this helps.

Originally posted by Selim Hendrickson:
Hi, I think I haven't made myself clear. Now, if there were a superclass instance ( which is not possible since A is abstract ) such as:
A a = new A();
and an instance of class C had access to private variables of "a" through accessor method x() then I wouldn't have been confused.
But first of all there is no instance of A. There is only one object of class type C which extends and inherits all members of abstract A except for the private variables. Then x() method is invoked using an instance of class type c which I believe should be invoking the inherited x() method of c. But how does the inherited method x() return x when x is not inherited. x() simply executes as if it still has a connection with A. Obviously I must be missing something, so I'd be gratefull if you could point the flaw in my reasoning. I'm tired I need to
Thanks ahead...
[ September 03, 2003: Message edited by: Selim Hendrickson ]
[ September 03, 2003: Message edited by: Selim Hendrickson ]

[ September 03, 2003: Message edited by: Larry Mathys ]

"There are only 10 types of people in the world, those who understand binary and those who don't."
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Larry.
_______________________________________________________________________

To be a bit more precise no object of type A is ever created. An instance of type C is created. The "super();" call in C's constructor makes the constructor of class A to initialize the part of the object C that is inherited from A. That is, A's constructor is executed on the object C.


SCJP2. Please Indent your code using UBB Code
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Private variables in superclass accessed by subclass
 
Similar Threads
Resizing, Dragging and Rotating
Confused about Dan's exam...
Inner class question from Dan's exam
protected member
post increment confusion