aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can a sub class reference access private variable of super class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can a sub class reference access private variable of super class?" Watch "can a sub class reference access private variable of super class?" New topic
Author

can a sub class reference access private variable of super class?

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
can a sub class reference access private variable of super class?
If possible write and show me one sample code..
If not why?


Abhishek
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, it most cases it can't.

The only exception would be to make an inner class a sublcass.
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks for the reply
see this sample code and tell me what is happening at line 1

public class Testing {
private int a;

public Testing(){
this.a=5; // line 1
System.out.println("super"+this.a);
this.changeA(9);
}

public void changeA(int a){
this.a=a;
}

static public void main(String[] args){
Test1 obj=new Test1();
}
}

class Test1 extends Testing{
private int b;
public Test1(){
System.out.println("this"+this.b);
}

public void changeA(int a){
this.b=7;
}
}

what i think is...
when we say this.a=5....since the current instance in execution is Test1 object.The Test1 class doesnot have varibale named "a" then the jvm will search for the variable in super class finds it and the value 5 will be assigned...but the variable "a" is declared private..

So, form this what i conclude is subclass reference can access the private variables of the superclass within the context of Superclass..
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I'm not sure how you are drawing that conclusion.

You are accessing the variable a in Testing from within an instance method in Testing.

You are not involving the subclass except to create an instance of it.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Abhi,

I can't accept what you said.
Abhi:when we say this.a=5....since the current instance in execution is Test1 object


"this" always refers to current object where you are. For your information there will be two objects.

1. Super class Object.
2. Child class Object.


public class Testing {
private int a;

public Testing(){
this.a=5;
// this always points to current object you are in.


System.out.println("super"+this.a);
this.changeA(9);
//base class ref.changeA(9); Here it is basic overriding concept
}

public void changeA(int a){
this.a=a;
}

static public void main(String[] args){
Test1 obj=new Test1();
}
}

Hope its clear.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
So, form this what i conclude is subclass reference can access the private variables of the superclass within the context of Superclass..


As Keith said, class Test1 never accesses a in Testing. Only code in Testing accesses a. a is indeed inaccessible from Test1.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Abhi,





1- Test1 is subclass of Testing.
2- In the main method you are creating an instance of the subclass Test1.
3- Inside the constructor of the parent, you are assigning 5 to the member variable a. "this" always refers to current instance.
4- You print the value of this.a.
5- changeA() is called, and because the subclass overrides changeA(), the subclass version will be called. (remember if the instance would be of
parent class this wont happen).
5- Value of "b" ofcourse of subclass Test1 is set to 7.

Agree with Keith, even subclass can't access the private member of the parent class except one exception and that is, if the an inner class becomes
subclass too. See the code:




Thanks and Regards,
cmbhatt


cmbhatt
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks for all ......i'am very much confused with "this" keyword..
can any one elaborate more here..
when i printed "this" reference in the Superclass constructor it printed class name "Test1" followed by its hashcode...
so, "this" refers to the current instance which in our case is instance of Test1 object..
so,
this(current instance i.e. Test1).a(private variable of Superclass)=5

is this not happening int the code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can a sub class reference access private variable of super class?