Answer by user982042 for Why use getters and setters/accessors?
If you don't require any validations and not even need to maintain state i.e. one property depends on another so we need to maintain the state when one is change. You can keep it simple by making field...
View ArticleAnswer by aditya lath for Why use getters and setters/accessors?
There is a difference between DataStructure and Object.Datastructure should expose its innards and not behavior.An Object should not expose its innards but it should expose its behavior, which is also...
View ArticleAnswer by Lakindu Hewawasam for Why use getters and setters/accessors?
From my experience, it is ideal to set variables as private and to provide accessors and modifiers to each variable.This way, you can create read only variables, or write only variables depending on...
View ArticleAnswer by Blasanka for Why use getters and setters/accessors?
Getters and setters coming from data hiding. Data Hiding means We are hiding data from outsiders or outside person/thing cannot access our data.This is a useful feature in OOP.As a example:If you...
View ArticleAnswer by Rakesh Singh for Why use getters and setters/accessors?
One relatively modern advantage of getters/setters is that is makes it easier to browse code in tagged (indexed) code editors. E.g. If you want to see who sets a member, you can open the call hierarchy...
View ArticleAnswer by kangalio for Why use getters and setters/accessors?
I know it's a bit late, but I think there are some people who are interested in performance.I've done a little performance test. I wrote a class "NumberHolder" which, well, holds an Integer. You can...
View ArticleAnswer by Jean-Christophe Blanchard for Why use getters and setters/accessors?
Although not common for getter and setter, the use of these methods can also be used in AOP/proxy pattern uses.eg for auditing variable you can use AOP to audit update of any value.Without...
View ArticleAnswer by Pritam Banerjee for Why use getters and setters/accessors?
Getters and setters are used to implement two of the fundamental aspects of Object Oriented Programming which are: AbstractionEncapsulationSuppose we have an Employee class:package...
View ArticleAnswer by Haakon Løtveit for Why use getters and setters/accessors?
EDIT: I answered this question because there are a bunch of people learning programming asking this, and most of the answers are very technically competent, but they're not as easy to understand if...
View ArticleAnswer by yegor256 for Why use getters and setters/accessors?
In a pure object-oriented world getters and setters is a terrible anti-pattern. Read this article: Getters/Setters. Evil. Period. In a nutshell, they encourage programmers to think about objects as of...
View ArticleAnswer by abarnert for Why use getters and setters/accessors?
You should use getters and setters when:You're dealing with something that is conceptually an attribute, but:Your language doesn't have properties (or some similar mechanism, like Tcl's variable...
View ArticleAnswer by GeZo for Why use getters and setters/accessors?
There is a good reason to consider using accessors is there is no property inheritance. See next example:public class TestPropertyOverride { public static class A { public int i = 0; public void add()...
View ArticleAnswer by Jonathan for Why use getters and setters/accessors?
If you want a readonly variable but don't want the client to have to change the way they access it, try this templated class:template<typename MemberOfWhichClass, typename primative> class...
View ArticleAnswer by MongooseLover for Why use getters and setters/accessors?
I can think of one reason why you wouldn't just want everything public.For instance, variable you never intended to use outside of the class could be accessed, even irdirectly via chain variable access...
View ArticleAnswer by Devrath for Why use getters and setters/accessors?
We use getters and setters:for reusabilityto perform validation in later stages of programmingGetter and setter methods are public interfaces to access private class members.Encapsulation mantraThe...
View ArticleAnswer by bobobobo for Why use getters and setters/accessors?
Code evolves. private is great for when you need data member protection. Eventually all classes should be sort of "miniprograms" that have a well-defined interface that you can't just screw with the...
View ArticleAnswer by R. Martinho Fernandes for Why use getters and setters/accessors?
A public field is not worse than a getter/setter pair that does nothing except returning the field and assigning to it. First, it's clear that (in most languages) there is no functional difference. Any...
View ArticleAnswer by Jorge Aguilar for Why use getters and setters/accessors?
Well i just want to add that even if sometimes they are necessary for the encapsulation and security of your variables/objects, if we want to code a real Object Oriented Program, then we need to STOP...
View ArticleAnswer by Mohamed for Why use getters and setters/accessors?
Don't use getters setters unless needed for your current delivery I.e. Don't think too much about what would happen in the future, if any thing to be changed its a change request in most of the...
View ArticleAnswer by andrers52 for Why use getters and setters/accessors?
From a object orientation design standpoint both alternatives can be damaging to the maintenance of the code by weakening the encapsulation of the classes. For a discussion you can look into this...
View Article