OnJava8-Examples/references/HorrorFlick.java

35 lines
811 B
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: references/HorrorFlick.java
2015-05-18 23:05:20 -07:00
// You can insert Cloneability
// at any level of inheritance.
2015-05-05 11:20:13 -07:00
class Person {}
2015-05-18 23:05:20 -07:00
2015-05-05 11:20:13 -07:00
class Hero extends Person {}
2015-05-18 23:05:20 -07:00
class Scientist extends Person
implements Cloneable {
2015-05-05 11:20:13 -07:00
public Object clone() {
try {
return super.clone();
} catch(CloneNotSupportedException e) {
2015-05-18 23:05:20 -07:00
// Should never happen; it's Cloneable:
2015-05-05 11:20:13 -07:00
throw new RuntimeException(e);
}
}
}
2015-05-18 23:05:20 -07:00
2015-05-05 11:20:13 -07:00
class MadScientist extends Scientist {}
public class HorrorFlick {
public static void main(String[] args) {
Person p = new Person();
Hero h = new Hero();
Scientist s = new Scientist();
MadScientist m = new MadScientist();
//! p = (Person)p.clone(); // Compile error
//! h = (Hero)h.clone(); // Compile error
s = (Scientist)s.clone();
m = (MadScientist)m.clone();
}
} ///:~