Unit Testing in Java: How Tests Drive the Code

Chapter 7: Inheritance and Polymorphism

Java is an object-oriented language, as we know. In addition to object identity and data encapsulation, inheritance and polymorphism are other important concepts of the object-oriented paradigm. [1] These two concepts have not played a major role in the previous chapters of this book, except in the implementation of our test cases. As useful and convenient as inheritance and polymorphism may be for software development, the problems they can cause during testing can be equally big. But all complaints are useless: we have to deal with the positive and negative effects.

7.1 Inheritance

Well-Shaped Inheritance Hierarchies

Many inexperienced developers consider inheritance between classes mainly as a practical means to simplify their implementation: once they know a class that possesses the capabilities they need for a new class, they often extend this class. After all, the key word is extends. Then they add a method here, override another one there, and voil the new marvel is ready.

Using the inheritance mechanism in this way has a harmful effect, mainly since we take both the desired and the undesired properties of the superclass. And because Java links a subtype relationship to the extends relationship, like most other statically typed languages, the door is wide open for unintended use of the new, derived class. Inheritance is commonly used as a reuse mechanism and is one of the main reasons for poor maintainability of larger object-oriented systems. [2]

The problems with this kind of inheritance can be avoided by observing the

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Programming Languages
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.