Software Design/Don't extend abstract class unless needed

From Wikiversity
Jump to navigation Jump to search

Checklist questions:

  • Does a class extend some abstract class because it needs to do it, but not just because it can do it?

Populating an abstract class with another implementation makes it harder to comprehend the logic which uses the abstract class: it may not be easy to understand for the reader which implementations can and can not be used from that logic.

More implementations for an abstract class make harder to change it: for example, to add a new function, or to change a signature of an existing function.

An object of the class may be mistakenly passed into a function expecting the abstract class, i. e. the robustness of such functions is reduced.

The fact that a class extends an abstract class without apparent purpose may puzzle the readers.

Why not

[edit | edit source]

If a class extends a well-known abstract class, the reader may be able to immediately know the principles of the workings of the class, without studying all its logic. For example, if a class implements Iterator abstract class core to the used programming language, the reader may be confident they understand the semantics of the code working with this Iterator implementation, even though they didn't read its code. In other words, extending an abstract class is a form of structure.