Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Statement Doubt

 
Sanjeev Narula
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also this statement:


Can a private method of super class be declared within sub classs ?

ans is Yes.
 
Sharan Ashok Vasandani
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sanjeev Narula
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic