Not all IS-A relationships should be modeled with Inheritance

Example: Square inherits from Rectangle

  • When we perform the change width operation on both objects Square will change its length, while Rectangle doesn’t.
    • When we perform obtain length operation. Square will return its changed length, while Rectangle returns unchanged length this violates OOP Compatibility

Fix: use a common interface

  • Abstract the Square & Rectangle properties into an interface, implement the width & length in the each of the class so we aren’t able to substitute object on the obtain length(Only available to the Reactangle object), thus ensuring OOP Compatibility
  • Make it easier to argue for the correctness of programs
  • We can make the Class Final or the individual Methods Final
  • java.lang.Math & java.lang.String cannot be inherited

Unit Testing