fork download
  1. public class Main
  2. {
  3. public static void main(String[] args) {
  4. Animal a = new Dog();
  5. Animal x = a.spawn();
  6.  
  7.  
  8. Dog b = new Dog();
  9. Dog y = b.spawn();
  10. }
  11.  
  12. }
  13.  
  14.  
  15. class Animal
  16. {
  17. protected int _generation = 0;
  18.  
  19. public Animal() {
  20. this(1);
  21. }
  22.  
  23. public Animal(int generation) {
  24. _generation = generation;
  25. }
  26.  
  27. public Animal spawn() {
  28. System.out.println("From Animal Spawn");
  29. return new Animal(_generation+1);
  30. }
  31. }
  32.  
  33.  
  34. class Dog extends Animal
  35. {
  36. public Dog() {
  37. this(1);
  38. }
  39.  
  40. public Dog(int generation) {
  41. _generation = generation;
  42. }
  43.  
  44.  
  45. @Override
  46. public Dog spawn() {
  47. System.out.println("From Dog Spawn");
  48. return new Dog(_generation + 1);
  49. }
  50. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
From Dog Spawn
From Dog Spawn