• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Accessibility

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone give an example of how an instance of a class can access the private features of another instance of the same class??
Thanks
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rana Thakur:


[This message has been edited by Sivalingam Sivasuthan (edited March 07, 2001).]
 
Rana Thakur
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Siva.....
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Siva , Rana
I don't see how one instance is accessing the private variable of the other instance. It is the method in which the variables are being accessed.Any instance variable can only be accessed by that particular instance ( object ) - So how can another object access it directly??
Your programme can be rewritten as :
class Feet{
private int iFeet; //Private Member of the Class Feet
//public Feet(){iFeet=0;} // NOT NEEDED - WHY PUT IT UNECESSARILY ???
public Feet(int ifeet){iFeet=ifeet;}

public static int CalDifference(Feet A, Feet B){
return(A.iFeet-B.iFeet); }
}
public class PrivateAccessTest{
public static void main(String[]args){
Feet A,B;
A=new Feet(50);
B=new Feet(25);
System.out.println("Different Between A and B is : "+Feet.CalDifference(A,B)+" feet ");
}
}
------------------
 
Sivalingam Sivasuthan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shailendra Guggali:
I am Sorry that default constructor is not relevant to this topic.
Reference A,B are different instants of the Same Class Feet. In CalDifference() method We are accessing the private variable of instance B directly(as feet.iFeed,notice that feet is an another object of the Same class).
-Siva.
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi siva
maybe I am not understanding something very simple, but
I still maintain that you can access the variable of an instance only with that object.
your example is totally misleading -
" In CalDifference() method We are accessing the private
variable of instance B directly(as feet.iFeed,notice that feet is an another object of the Same class)."
whether you call the object " feet or hand "- it is only a reference to the copy of the object reference you are passing to it in the method.
 
Rana Thakur
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Shailendra you do have a point there. I read the same statement couple of places and took it to be granted that it is true. One place where I came across this one was Velmurugan Periasamy's notes at page 16 under Modifer topic
"� These modifiers dictate, which classes can access the features. An instance of a class can access the private features of another instance of the same class."
In the example Siva has given, the two objects are accessing their own private members.
What I understand is private variables are the property (state) of an object and there is no way another object can access them.
(thats why they are private!!)
so either the statement is wrong or I am missing out something.
 
Rana Thakur
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe what siva means is that (in his example), when I call A.CalDifference(B) then CalDifference () of A is being invoked. Inside that function the value of private variable iFeet of object A can be accessed (this.iFeet) and the value of private variable iFeet of object B can be obtained too (feet.iFeet).
Thus in this function not only I can access the iFeet of A (which is invoking this function) but also the private field i.e. iFeet of object B.
And of course, other than the class Feet, say in class PrivateAccessTest, if one tries to access the private variable of any object the compiler will complain.

[This message has been edited by Rana Thakur (edited March 07, 2001).]
 
Sivalingam Sivasuthan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rana Thakur:
Or maybe what siva means is that (in his example), when I call A.CalDifference(B) then CalDifference () of A is being invoked. Inside that function the value of private variable iFeet of object A can be accessed (this.iFeet) and the value of private variable iFeet of object B can be obtained too (feet.iFeet).
Thus in this function not only I can access the iFeet of A (which is invoking this function) but also the private field i.e. iFeet of object B.
And of course, other than the class Feet, say in class PrivateAccessTest, if one tries to access the private variable of any object the compiler will complain.


Rana Thakur you are right that is what I tried to show you in my example.
-Siva
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic