Search This Blog

Monday, June 9, 2008

Design Thoughts - Protected vs Private

1) if you didn't design it to be extended, i,e., you didn't design it to be abstract, then all class attribute should be private. Sometimes I would create a publicly accessible class and think to myself... "self, let's make the private members protected in case someone needs to extend upon this class in the future."... well, I have now come to realize that that is a bad design choice. It breaks encapsulation. I now think that if a class is intended to be extended then the class itself should be abstract and its members (that need to be available to subclasses) obviously be protected. A class should be designed to be extended, if it's not designed to be extended then its members should be private (when there is no obvious choice but to make them private, of course.) Just a thought...

No comments: