• 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

Private means

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can one object acess a private variable of another object of the same class ?

Ans: Yes

Could any one explaine this rule with one example?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example



and



When run, the following results occur



Hope that helps.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"shree shree"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
whots wrong in my name ?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to have your real first and real last names, not one or the other repeated.

Funny, you fixed it as I posted this. Your name now complies. Thanks.

Mark
[ April 06, 2005: Message edited by: Mark Spritzler ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark

I am not able to understand this concept. The code belows uses two different classes and objects of these classes to access a private variable.



Forgot to attach the output.

getanotherName: Hello
a.getName: Hello
b.getName: null
[ April 06, 2005: Message edited by: Anupam Sinha ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anupam,
See variables are never overridden, they are hidden. This means that if the variables are called under class A's scope, variable of class A will be visible. While if its under class B's scope, variable of class B will be visible.
So here since "name" variable is initialized to "World" in class B but this is hidden in class A, in A the same variable is null. Class A cannot see class B's varaible. So it displays null.
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Animesh
1. Class B has getName() method. Bcoz it is inherited from A.
2. Now class B object called the getName() method: b.getName().
3. B has own memeber variable b and initialized as world
4. Why it is print null.


So here since "name" variable is initialized to "World" in class B but this is hidden in class A, in A the same variable is null.



But the below code is work fine.


Output:
A value From A Scope 100
B value From B Scope 200
A value From B scope 100

How is it possible.
I am Confused. Is anything I understand wrong?
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So whats the problem u r having, i guess its in this statement
System.out.println("A Value From B Scope " +b.getA());

right?
Ok whats happening here is that since methods are inherited from super class to sub class, so u can very well access the method getA(). Now since this method accesses the variable a, which is initialized to 100, u get the output. Now one more thing, see when u create an instance of a class, beore the instance is created constructor of super class is also run and in effect all its instance initializers. So lets see whats happening here

When u create an object of A, u also initialize the variable a to 100;
When u create an object of B, u initailize A's instance members( A is super class of B, here a is initialized to 100) as well as instance members of class B
U getting it?
So when getA() is called from Class A, u get the variable 100 printed for a.

Now lets take up the previous example for u.
In that example :

See above, i have modified the code for u.
When u create an instance of Class A, u get name initialized for the class.
When u create an instance of Class B, the no-arg constructor of class A is also run because by default JVM inserts the statement "super()". In ur earlier case, since A's default constructor was being run, u got the value as null for the string name.
But now look at the code i have given, here in the no-arg constructor of A, i am also initializing the variable name of A.
Run the program and tell me if u have any problem in justifying the output.

Thanks
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Animesh
Thanks lot. Now I am cleared in Inheritance with member hiding.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Animesh

Thanks for the reply. I wanted to know that even when a (Object ref. to A) and b (Object ref to B) belong to two different classes then also



the above line executes.

In the code above an Object of type B is changing an Object of Type A.

So does it mean "Can one object acess a private variable of another object of the same class and a diferent class aswell.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So does it mean "Can one object acess a private variable of another object of the same class and a diferent class aswell.


No an object cannot directly access the private variable of another object of the same class. It can only be done using a public method which accesses the variable , like its done using anotherName() method
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Animesh]: No an object cannot directly access the private variable of another object of the same class.

I guess that means you haven't actually tried it, eh?
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jim,
Is it possible? how ?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animesh: No an object cannot directly access the private variable of another object of the same class.

The example given by Mark has demonstrated that it's possible.

Extracted from the second post of this thread.

[ April 07, 2005: Message edited by: Joyce Lee ]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animesh and Raghu gave really good explanations there.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic