[Tutor] Class access rules

Alan Gauld alan.gauld at yahoo.co.uk
Mon Mar 7 06:30:01 EST 2022


On 07/03/2022 00:59, Phil wrote:
> 
> On 7/3/22 11:42, Mats Wichmann wrote:
>>
>> Why?
> 
> Just for the sake of correctness, that's all.
> 
There is nothing correct about getters and setters,
in fact there's a strong argument for saying they
are a fundamental error on OOP. They were introduced
in Java as part of the Javabeans definition so that
IDEs and GUI builders could use them to create working
GUIs on the fly.

In good OOP design you should ideally not need to
access the internal attributes of a class at all. The
methods should do all the work and the attributes are only
there internally to support those methods. In practice
of course objects often represent data stores and the
attributes are of interest. But the goal should be
to minimize that and not to expose them.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list