I have a little class. I have some variables which I would like to access from other classes. IS there any point in writing get/set for this? Why not only make the attribut public and skip the get/set method.
Printable View
I have a little class. I have some variables which I would like to access from other classes. IS there any point in writing get/set for this? Why not only make the attribut public and skip the get/set method.
use it as a property, in your class..
you can set each variable property to accept or only return values.
// am assuming here that you know how to make them into a property :)
Because of encapsulation, the fundamental property of OOP. There is big difference between the class's protocol - how it looks out, which methods exposes to outer world, and the class implementation - which fields it contains and how it deals with them. Nothing outside you class, including other your class, have not to take care about the class implementation.
Believe us, this is not only academic excercise. If you would use getter/setter rather then public fields, you will avoid many problems in future.