Which of the following are legal clone()
methods in a class called MyClass that extends Object?
A. public Object clone() { return super.clone(); } /* w ww . ja va 2 s .co m*/ B. public Object clone()throws CloneNotSupportedException { return super.clone(); } C. public MyClass clone() { return (MyClass)super.clone(); } D. public MyClass clone() throws CloneNotSupportedException { return (MyClass)super.clone(); }
B, D.
The CloneNotSupportedException must be dealt with, so A and C are wrong.
The version being overridden (in Object) has return type Object, so prior to release 5.0 the return type in D would be illegal; however, now that covariant returns are legal, D is allowed.