Polymorphic copy: Difference between revisions

Line 551:
 
=={{header|Java}}==
Here we implement a "copy" method once in the base class because there is by default no public way to copy an object from outside the class in Java. (There is a protected, not public, "clone" method inherited from Object.) The way to clone polymorphically is to obtain a copy from the superclass's <code>clone()</code> method, and then perform custom copying operations specific to this class. If this pattern is followed, the calls will eventually pass all the way up to <code>Object</code>'s <code>clone()</code> method, which performs a polymorphic copy. If you do not follow this pattern, and simply use a constructor, like <code>new T()</code>, then the copy won't be polymorphic.
<lang java>class T implements Cloneable {
public String name() { return "T"; }
Anonymous user