You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.2 KiB

  1. public class TestFruits
  2. {
  3. public static void main(String[] args)
  4. {
  5. Apple a = new Apple(2);
  6. System.out.println(Apple.count);
  7. System.out.println();
  8. Fruit[] fr = new Fruit[3];
  9. fr[0] = a;
  10. fr[1] = new RedDelicious();
  11. fr[2] = new Fruit(4);
  12. System.out.println();
  13. System.out.println("Using array fr: ");
  14. for(int i = 0; i < fr.length; i++)
  15. {
  16. fr[i].display();
  17. System.out.println(Apple.count);
  18. System.out.println();
  19. }
  20. }
  21. }
  22. class Fruit
  23. {
  24. public int f;
  25. public Fruit(int f)
  26. {
  27. System.out.println("Fruit");
  28. this.f = f;
  29. }
  30. public int getF()
  31. {
  32. return this.f;
  33. }
  34. public void f1()
  35. {
  36. System.out.println("FRUIT f1");
  37. }
  38. public void f2()
  39. {
  40. f1();
  41. System.out.println("FRUIT f2");
  42. }
  43. public void display()
  44. {
  45. System.out.println("display method from Fruit invoked");
  46. f2();
  47. }
  48. }
  49. class Apple extends Fruit
  50. {
  51. public static int count = 0;
  52. public Apple(int a)
  53. {
  54. super(a);
  55. System.out.println("Apple");
  56. count = count + getF();
  57. }
  58. public void f1()
  59. {
  60. System.out.println("Apple f1");
  61. }
  62. public void f2()
  63. {
  64. f1();
  65. System.out.println("Apple f2");
  66. super.f1();
  67. }
  68. public void display()
  69. {
  70. System.out.println("display method from Apple invoked");
  71. f2();
  72. }
  73. }
  74. class RedDelicious extends Apple
  75. {
  76. public static int count;
  77. public RedDelicious()
  78. {
  79. super(3);
  80. System.out.println("RedDelicious");
  81. count = count + getF();
  82. }
  83. public void f1()
  84. {
  85. System.out.println("RedDelicious f1");
  86. }
  87. public void f2()
  88. {
  89. f1();
  90. System.out.println("RedDelicious f2");
  91. super.f1();
  92. }
  93. public void display()
  94. {
  95. System.out.println("display method from RedDelicious invoked");
  96. f1();
  97. }
  98. }