Private variable problem
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi All,
I want to access a varible form my super class. But i am having problem to access that.
Below is my code :
If i declare varible as 'Private' then i am not able to access it but when i declare it as 'Protected' then it is accesible.
I dont want to break the encapsulation.
How can i access it when it is declared as Private.
am i undersatnding correct it or i am doing something wrong ?
Please shed some light on it.
Thanks
Joseph
I want to access a varible form my super class. But i am having problem to access that.
Below is my code :
If i declare varible as 'Private' then i am not able to access it but when i declare it as 'Protected' then it is accesible.
I dont want to break the encapsulation.
How can i access it when it is declared as Private.
am i undersatnding correct it or i am doing something wrong ?
Please shed some light on it.
Thanks
Joseph
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You will need to change A to provide access either by making the variable protected or by adding an accessor method e.g. :
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You can use getters & setters to access that variable.
Joseph yan
Greenhorn
Posts: 3
posted 16 years ago
Thanks for your replies ..
How do i do with getter and setter ?
Can you please provide some example .
Please note only problem is i dont want to break encapsulation.
if i will use protected then varible is visible out side the class.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks for your replies ..
How do i do with getter and setter ?
Can you please provide some example .
Please note only problem is i dont want to break encapsulation.
if i will use protected then varible is visible out side the class.
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Joseph:
You probably just want a getter. Just move your getPrice() method from class A to class B. Extending classes already breaks encapsulation. If encapsulation is what you are concerned about, you should probably use composition rather than inheritance.
John.
You probably just want a getter. Just move your getPrice() method from class A to class B. Extending classes already breaks encapsulation. If encapsulation is what you are concerned about, you should probably use composition rather than inheritance.
John.
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Paul Sturrock has provided an excellent example which maintains encapsulation.Joseph yan wrote:How do i do with getter and setter ?
Can you please provide some example .
| money grubbing section goes here: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








