Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This is a stub. It needs an example of an error caused due to by using the Base base class's object where the subclass's object was expected. It should not produce a RuntimeException (ClassCastException) to qualify.

When a non-final nonfinal class defines a clone() method that does not call super.clone(), cloning a subtype will produce an object of the wrong type.

...

This compliant solution correctly calls super.clone() in the Base class's clone() method.

Code Block
bgColor#ccccff
class Base implements Cloneable {
  public Object clone() throws CloneNotSupportedException {
    return super.clone();	 
  }
  protected void doLogic() {
    System.out.println("Superclass doLogic");
  }
}

class Derived extends Base {
  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
  protected void doLogic() {
    System.out.println("Subclass doLogic");
  }
  public static void main(String[] args) {
    Derived dev = new Derived();
    try {
      Base devClone = (Base)dev.clone(); // has type Derived, as expected
      devClone.doLogic();  // prints "Subclass doLogic", as expected
    } catch (CloneNotSupportedException e) { /* ... */ }
  }
}

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8c4f2535b0e2a7ef-d78a3bf1-4cfa466f-8ca3b1d9-03c0669ada768fb4785a484d"><ac:plain-text-body><![CDATA[

[[API 2006

AA. References#API 06]]

[Class Object

http://java.sun.com/javase/6/docs/api/java/lang/Object.html]

]]></ac:plain-text-body></ac:structured-macro>

...