I have a variable in a class. How can i access that variable from another class?
I have a variable in a class. How can i access that variable from another class?
Unlesss you make that variable public, you can not access that variable from another class. Other methods are there too, like make it nprotected and inhet the class.
But if you make the variable private, you can not access the variable from outside the class. May it be another class/function or main(). That is the fundamental principal of OOPS.
-Kalayama
[COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
But We can access a private variable from an outside class through a public method in that class. right?
You are right. The question is directly accessing the variable. Can you change the value of the variable from another class? You can not do that unless you have a function defined for it in this class.
What you are actually accessing is the function of that class not the variable.
PS: Sorry if the post sounds shabby. Scribbled it in a hurry.
Cheers!
Kalayama
[COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
there is an no.of ways to access it
1.with the help of inheritance
2.public way of declaring
3.access with the help of objects
yeah.. No way of directly accessing it.
These answers naive. Find the correct things below.there is an no.of ways to access it
1.with the help of inheritance
2.public way of declaring
3.access with the help of objects
1)with the help of inheritance, you can only access protected variable (apart from public). You can not access private variable even if you inherit it.
2)public way of declaring is not opps compiant. If the variables are declared public, one can as well declare them as strcutures. Yes. In this way you can access the class variable though.
3)????? where do objects come from???? an object is nothing but instance of class!!! if you declare the variable as private, you can not access it with an object of the class. (of course if you have defined a function to access it, you can access the function that function and then indirectly you can access the variable. )
sweetsage_2007, just wanted to correct you mate. The information provided by you was not complete. Hope to see some great posts from you in future. Cheers! kalayama
Last edited by kalayama; 03-08-2007 at 08:17 AM. Reason: Formatting went to the dogs after I posted it; corrected it
[COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"