• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Statement Doubt

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also this statement:


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

ans is Yes.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic