Home » Uncategorized

Extending Non-Abstract Classes

30 December 2003 No Comment

Many people create APIs that extends concrete (non-abstract) classes. I have come to avoid this practice. Through the use of design patterns (especially strategy) I have come up with much better designs. It is my preference that classes are either final or abstract. If they are final, nobody can extend them. If they are abstract, then all of their methods should be either final or abstract.

Why shouldn’t you extend a concrete class and override one of its methods? It breaks encapsulation. You have to know about the innards of the class that you are extending. Just because you have an IS-A relationship to another class doesn’t mean that you shouldn’t worry about encapsulation.


Comments are closed.