Professional Documents
Culture Documents
05 Abstraction 1
05 Abstraction 1
05 Abstraction 1
Abstraction 1
Mostafa S. Ibrahim
Teaching, Training and Coaching since more than a decade!
Img Src
Rectangle as a class
Accessor & mutator functions
● Accessors = Getters, Mutators = Setters
● An accessor is a class function used to access/read data members
○ We used GetWidth as an accessor
● A mutator is a class method used to mutate/change data members.
○ We used SetWidth as a mutator
● Your IDE may provide you with generation for them (e.g. as in eclipse)
Accessor & mutator: Eclipse
● Put cursor in the class, then click Alt+Shift+S ⇒ Menu appear
○ Select from menu: Generate setters and getters
○ Decide which setters & getters should be given
Accessor & mutator: Eclipse
● You can also change style of generation (e.g. getWidth or GetWidth)
● Go to Eclipse → Window →Preferences
Accessor & mutator: The proper way?
● There is a debate around setters and getters
○ Are they evil or not? Should we avoid as possible? Really break OOP encapsulation?
● Consider the following tips
○ Don’t be the guy who generate setters & getters for every field.
■ For setters: This can cause problems. E.g. Inject wrong data through setters
■ For getters: Exposing implementation details can cause code issues
○ Think twice what to expose with non-data-only objects
○ “Don’t ask for the information you need to do the work; ask the object that has the information
to do the work for you.” Allen Holub
“Acquire knowledge and impart it to the people.”