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: 2341
posted
0
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
posted
0
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,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35256
7
posted
0
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.
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
posted
0
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