Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can a sub class reference access private variable of super class?
If possible write and show me one sample code..
If not why?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it most cases it can't.

The only exception would be to make an inner class a sublcass.
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic