From: Robert Klemme Date: 2006-06-03T21:02:24+09:00 Subject: Re: OO / Private verse Protected 2006/6/1, Joost Diepenmaat : > That's not what I meant. You can still make methods private. But if methods > are useful enough to make protected / need to be accessible from subclasses > or other instances, you should really stop and think if you _have_ to deny > access from the rest of the code. If your classes are only going to be used > in one project, knock yourself out - you can always change it later. But if > you are writing reusable components... well, I think I've made my point :-) Note though that it's usually harder to remove functionality than to add it, i.e. it's better to start with the more restricted version (protected) and make methods public later if you see the need for this. Nevertheless all sorts of problems arise if library code that is inherited from is changed (e.g. if you make a protected method public sub classes will break in Java because you cannot reduce visibility in an overridden method). An idiom I use from time to time makes explicit use of "protected": I have an abstract base class that implements algorithms that rely on specific parts implemented in sub classes; often these "parts" are just getters that return a piece of data - but it can be more complex of course. These things do not belong into the full light of public because they are not the methods intended for primary use. But I do grant you that they must be viewed as part of the public interface because they can cause dependencies - something which cannot happen with private methods in Java. Inheritance is an interesting but also complex field and difficult to get right. You said earlier: "I cann't think of any language that allows you to do that." Off the top of my head Eiffel is one of the languages that does this. Bertrand Meyer thinks that implementation inheritance is as good as interface inheritance and that's why sub classes can completely change the public interface of their base classes. Of course then there is no longer an "is a" relationship and you cannot use sub class instances where base class instances were allowed. Btw, even if you don't agree with all of his positions "Object-oriented Software Construction" is nevertheless an interesting and valuable read. Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/