public class Main
{
public static void main
(String[] args
) { Animal a = new Dog();
Animal x = a.spawn();
Dog b = new Dog();
Dog y = b.spawn();
}
}
class Animal
{
protected int _generation = 0;
public Animal() {
this(1);
}
public Animal(int generation) {
_generation = generation;
}
public Animal spawn() {
System.
out.
println("From Animal Spawn"); return new Animal(_generation+1);
}
}
class Dog extends Animal
{
public Dog() {
this(1);
}
public Dog(int generation) {
_generation = generation;
}
@Override
public Dog spawn() {
System.
out.
println("From Dog Spawn"); return new Dog(_generation + 1);
}
}
cHVibGljIGNsYXNzIE1haW4gCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCQlBbmltYWwgYSA9IG5ldyBEb2coKTsgIAoJCUFuaW1hbCB4ID0gYS5zcGF3bigpOwogCiAKCQlEb2cgYiA9IG5ldyBEb2coKTsKCQlEb2cgeSA9IGIuc3Bhd24oKTsJCQoJfQoKfQoKCmNsYXNzIEFuaW1hbCAKewoJcHJvdGVjdGVkIGludCBfZ2VuZXJhdGlvbiA9IDA7CgoJcHVibGljIEFuaW1hbCgpIHsKCQl0aGlzKDEpOwoJfQoJCglwdWJsaWMgQW5pbWFsKGludCBnZW5lcmF0aW9uKSB7CgkJX2dlbmVyYXRpb24gPSBnZW5lcmF0aW9uOwoJfQoKCXB1YmxpYyBBbmltYWwgc3Bhd24oKSB7CgkJU3lzdGVtLm91dC5wcmludGxuKCJGcm9tIEFuaW1hbCBTcGF3biIpOwoJCXJldHVybiBuZXcgQW5pbWFsKF9nZW5lcmF0aW9uKzEpOwoJfQp9CgoKY2xhc3MgRG9nIGV4dGVuZHMgQW5pbWFsIAp7CQoJcHVibGljIERvZygpIHsKCQl0aGlzKDEpOwoJfQoJCglwdWJsaWMgRG9nKGludCBnZW5lcmF0aW9uKSB7CgkJX2dlbmVyYXRpb24gPSBnZW5lcmF0aW9uOwkJCgl9CgkKCglAT3ZlcnJpZGUKCXB1YmxpYyBEb2cgc3Bhd24oKSB7CgkJU3lzdGVtLm91dC5wcmludGxuKCJGcm9tIERvZyBTcGF3biIpOwoJCXJldHVybiBuZXcgRG9nKF9nZW5lcmF0aW9uICsgMSk7Cgl9Cn0=