Advertisement
Artcik

inheritance

Nov 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. ANIMAL:
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. package inheritance;
  9.  
  10. /**
  11. *
  12. * @author Arthur
  13. */
  14. public class Animal {
  15.  
  16. private String name;
  17. private int brain;
  18. private int body;
  19. private int size;
  20. private int weight;
  21.  
  22. public Animal(String name, int brain, int body, int size, int weight) {
  23. this.name = name;
  24. this.brain = brain;
  25. this.body = body;
  26. this.size = size;
  27. this.weight = weight;
  28. }
  29.  
  30. public void eat() {
  31. System.out.println("Animal.eat() called");
  32. }
  33.  
  34. public void move(int speed) {
  35. System.out.println("Animal.move() called. Animal is moving at " + speed);
  36. }
  37.  
  38. public String getName() {
  39. return name;
  40. }
  41.  
  42. public int getBrain() {
  43. return brain;
  44. }
  45.  
  46. public int getBody() {
  47. return body;
  48. }
  49.  
  50. public int getSize() {
  51. return size;
  52. }
  53.  
  54. public int getWeight() {
  55. return weight;
  56. }
  57.  
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65. DOG:
  66.  
  67. /*
  68. * To change this license header, choose License Headers in Project Properties.
  69. * To change this template file, choose Tools | Templates
  70. * and open the template in the editor.
  71. */
  72. package inheritance;
  73.  
  74. /**
  75. *
  76. * @author Arthur
  77. */
  78. public class Dog extends Animal {
  79.  
  80. private int eyes;
  81. private int legs;
  82. private int tail;
  83. private int teeth;
  84. private String coat;
  85.  
  86. public Dog(String name, int size, int weight, int eyes, int legs, int tail, int teeth, String coat) {
  87. super(name, 1, 1, size, weight);
  88. this.eyes = eyes;
  89. this.legs = legs;
  90. this.tail = tail;
  91. this.teeth = teeth;
  92. this.coat = coat;
  93. }
  94.  
  95. private void chew() {
  96. System.out.println("Dog.chew() called");
  97. }
  98.  
  99. @Override
  100. public void eat() {
  101. System.out.println("Dog.eat() called");
  102. chew();
  103. super.eat(); //To change body of generated methods, choose Tools | Templates.
  104. }
  105.  
  106. public void walk() {
  107. System.out.println("Dog.walk() called");
  108. move(5);
  109. }
  110.  
  111. public void run() {
  112. System.out.println("Dog.run() called");
  113. move(10);
  114. }
  115.  
  116. private void moveLegs(int speed) {
  117. System.out.println("Dog.moveLegs() called");
  118. }
  119.  
  120. @Override
  121. public void move(int speed) {
  122. System.out.println("Dog.move() called");
  123. super.move(speed);
  124. moveLegs(speed);
  125. }
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement