When reading various Stack Overflow questions and others' code the general consensus of how to design classes is closed. This means that by default in Java and C# everything is private, fields are final, some methods are final, and sometimes classes are even final.
The idea behind this is to hide implementation details, which is a very good reason. However with the existence of protected
in most OOP languages and polymorphism, this doesn't work.
Every time I wish to add or change functionality to a class I'm usually hindered by private and final placed everywhere. Here implementation details matter: you're taking the implementation and extending it, knowing full well what the consequences are. However because I can't get access to private and final fields and methods, I have three options:
- Don't extend the class, just work around the problem leading to code that's more complex
- Copy and paste the whole class, killing code reusability
- Fork the project
Those aren't good options. Why isn't protected
used in projects written in languages that support it? Why do some projects explicitly prohibit inheriting from their classes?
protected
is not a level of access that's betweenpublic
andprivate
.internal
is better for that. The difference betweenprotected
andpublic
lies in how you access, not who can access. – Joh Jul 14 '11 at 18:33