Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private variables in superclass accessed by subclass

 
Selim Hendrickson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Selim Hendrickson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic