| Author |
Statement Doubt
|
Sanjeev Narula
Greenhorn
Joined: Mar 16, 2007
Posts: 19
|
|
Can one object access a private variable of another object of the same class ? Ans is Yes Can anyone justify this statement with example ?
|
 |
Sanjeev Narula
Greenhorn
Joined: Mar 16, 2007
Posts: 19
|
|
And also this statement: Can a private method of super class be declared within sub classs ? ans is Yes.
|
 |
Sharan Ashok Vasandani
Greenhorn
Joined: Feb 22, 2007
Posts: 12
|
|
hi, as the subclass does not have any idea about private method of the superclass,hence it can declare its own method with same name as in superclass. this is answer for your second question.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi Sanjeev, 1- If the super class has private member (method or member variable) sub class has no info of that so sub class can declare it. 2- If the super class version is not private, the subclass shadows the member variables and overrides the methods. 3- If the super class has final method and it is private too, subclass can define it because it is not visible in the subclass. 4- static members are not overridden although can be redefined. No polymorphic behavior. Thanks and Regards, cmbhatt
|
cmbhatt
|
 |
Sanjeev Narula
Greenhorn
Joined: Mar 16, 2007
Posts: 19
|
|
Thanks for explaining second statement. Can you explain First Statement also "Can one object access a private variable of another object of the same class ? " Ans is Yes
|
 |
Gaurav Pavan Kumar Jain
Ranch Hand
Joined: Mar 19, 2007
Posts: 168
|
|
Hi Sanjeev One object cannot access a private vairable of another object of same class. It is because private vairable is object level vairable not class level(static) that vairable belongs to that particular object. Example: - class Stud { public static void main(String args[]) { private int a=10; Stud st=new Stud(); Stud s1=new Stud(); st.a=1; //1 will be the output st1.a=2; // not legal } }
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
"Can one object access a private variable of another object of the same class ? "
TRY THIS CODE: Caution: This is only applicable when you are accessing private member using the reference variable of the class from main() method defined in the same class. Try this from other class, you will get compilation error. Is this answer to your doubt? Thanks and Regards, cmbhatt [ April 05, 2007: Message edited by: Chandra Bhatt ]
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi Gaurav, Revise your code! There is one compilation error. 1- A local variable can't be with any access modifier, it can be only final. 2- "st1.a=2; // not legal" Why isn't it legal?
It is legal to access the private member of the class using the reference variable of the class only and only when you are doing this inside the method of the same class, in your case it is main. So it is legal. You should search the posts, There has been a lot discussion over this topic.
Thanks and Regards, cmbhatt
|
 |
 |
|
|
subject: Statement Doubt
|
|
|