...
Code Block |
---|
class Base implements Cloneable { public Object clone() throws CloneNotSupportedException { return new Base(); } protected static void doLogic() { System.out.println(""Superclass doLogic""); } } class Subclass1 extends Base { public Object clone() throws CloneNotSupportedException { return super.clone(); } protected static void doLogic() { System.out.println(""Subclass doLogic""); } public static void main(String[] args) { Subclass1 s = new Subclass1(); try { Object sc = s.clone(); // get's Base obj instead of subclass' System.out.println(sc.getClass().hashCode()); // a possible mistake // ((Subclass1)sc).doLogic(); // Produces ClassCastException, disqualified } catch (CloneNotSupportedException e) { /* ... */ } } } |
...
MET34-J. Follow the general contract when implementing the compareTo method 12. Methods (MET) 13. Exceptional Behavior (EXC)