Skip to content
Preptima
mediumConceptDesignEntryMidSenior

Where does encapsulation or polymorphism change a design, and how do you decide between composition and inheritance?

6 min readUpdated 2026-08-15

Answer

Short answer

Encapsulation earns its place when a class owns an invariant that callers would otherwise each have to enforce, and polymorphism when a conditional grows with every new requirement.

Was this answer helpful?

Likely follow-ups

  • What does Liskov substitution give you beyond "the subtype should make sense"?
  • How would you enforce that invariant if the object has to be reconstructed from a database row?
  • When is a package-private constructor a better boundary than a public one?
  • Your rule list has grown to thirty implementations. What breaks, and what would you do about it?

Related questions

Further reading