3.1 Typing in Java
We’ve all come to rely on the “safety” of compile-time type-checking. But, safety in type safety is as comforting as security in Social Security.
Suppose we have a class Car
with a year and an Engine class, and we want to
implement the ability to clone objects of this class. We’ll ignore deeper issues with
cloning in Java—see my article “Why Copying an Object Is a Terrible Thing to Do”
in Appendix 1, Web Resources. To clone, we implement the
Cloneable interface and provide a public
clone
method. Object’s
clone
can make a shallow
copy of the object. However, we want different instances of Car to
have different Engines. Therefore, we clone the Car using the base method but tweak it a little ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access