This Atom is Under Construction

Ordinary classes and functions work with specific types. If you want code that works across more types, this rigidity can be overconstraining.

Polymorphism is an object-oriented generalization tool. You write a function that takes a base class object as an argument, then use that function with any class derived from the base class—including classes that haven’t been created yet. Now your function is more general, and useful in more places.

A single hierarchy can be too limiting because you must inherit from that hierarchy to produce an object that fits your function argument. If a function argument is an interface instead of a class, the limitations are loosened to include anything that implements the interface. This gives the client programmer the option of implementing an interface in combination with an existing class—that is, to adapt an existing class to fit your function. Interfaces cut across class hierarchies, as long as you have the option to implement those interfaces to fit.

Sometimes even an interface is too restrictive. It still requires that your code works with that particular interface. You can write even more general code if you make it work with “some unspecified type,” rather than a specific interface or class.

End of sample. See for full early-access book.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.