Question 5
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
class Config implements Cloneable {
String env = "prod";
}
public class Main {
public static void main(String[] args) throws Exception {
Config c = new Config();
Config copy = (Config) c.clone();
System.out.println(copy.env);
}
}
- A Config must not implement Cloneable
- B Object.clone() is protected, and Config does not override it publicly, so Main cannot call it on a Config Correct
- C env must be final
- D The cast is wrong
Correct Answer: Option B
javac: "clone() has protected access in Object". Override it as public in Config.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!