Object-oriented – Modularity vs Polymorphism (OOD main concepts)

object-orientedobject-oriented-designpolymorphism

Booch gives 4 main concepts (principles) of OOD (see his book Object-oriented analysis and design with applications ):

  1. Abstraction
  2. Encapsulation
  3. Modularity
  4. Hierarchy

Usually we encounter to these 4:

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance

Are those equivalent? Should I understand Modularity as another term for Polymorphism? As from Booch's definition I don't have that feeling:

Modularity is the property of a system that has been decomposed into a
set of cohesive and loosely coupled modules.

As much as I understand here modules are like Java packages. I don't know if this is what Booch means.

Best Answer

the OO Principles you mention in the first list are guidelines for program design. (there are others aside from those too; SOLID for example.)

OO Principles are a collection of ideas and part of the OO mindset; understanding OO principles such as Abstraction, Encapsulation and Modularity will help you make good design decisions when faced with the task of creating new classes in code.

Some of those decisions might include

  • What name do I choose for a class or Method?
  • Which class does a Method or Field/Property belong to?
  • Should I add this line of code to a Method or create a new one?
  • Should a class be split into multiple separate classes?
  • Should two classes be merged together?
  • Which other classes should have access to a class' interface?

On the other hand, Polymorphism and Inheritance are not principles but language tools; just as for and while are language tools.

While they are often cited as being the defining language features of OO Programming, that line of thinking is somewhat misguided because they can be used in ways which grind against a lot of OO principles. A lot of exceptionally good OO code has been written without using inheritance or polymorphism.

There are occasions when inheritance and polymorphism are useful tools, just as there are occasions when do..while or switch are useful tools; and you should certainly understand what they are and how/when to use them.

However it's a mistake to consider either Inheritance or Polymorphism as OO principles, or that they are 'goals' to work towards. The reality is that they can and frequently are abused, resulting in bad code (usually by programmers who assume that any use of inheritance is a good thing, or who treat inheritance as their go-to tool for code reuse).