Posted By

bcmoney on 08/17/12


Tagged

Reflection inheritance


Versions (?)

Inheritance example


 / Published in: Java
 

This example demonstrates advanced object-oriented concepts including inheritance, getters/setters, method overloading and reflection.

  1. //////////////////////////////////////////////////////////
  2. /**
  3.  * Animal.java
  4.  * Represents a single Animal.
  5.  */
  6. public class Animal
  7. {
  8. private String name;
  9. private String habitat;
  10.  
  11. /* constructor - Sets up the Animal with a name
  12.   * @param animalName String
  13.   */
  14. public Animal(String animalName)
  15. {
  16. name = animalName;
  17. }
  18.  
  19. /* constructor - Sets up the Animal with a name
  20.   * @param animalName String
  21.   */
  22. public hasHabitat(String animalHabitat)
  23. {
  24. habitat = animalHabitat;
  25. }
  26.  
  27. /* toString - Returns this Animal's name as a string
  28.   * @return name String
  29.   */
  30. public String toString()
  31. {
  32. return name;
  33. }
  34. }
  35.  
  36.  
  37. //////////////////////////////////////////////////////////
  38. /**
  39.  * Fish.java
  40.  * Represents an animal of type Fish.
  41.  */
  42. import Animal;
  43.  
  44. public class Fish extends Animal
  45. {
  46. private String name;
  47.  
  48. /* constructor - Sets up the Animal with a name
  49.   * @param animalName String
  50.   */
  51. public Animal(String animalName)
  52. {
  53. name = animalName;
  54. }
  55.  
  56. public hasHabitat()
  57. {
  58.  
  59. }
  60.  
  61. public canSwim()
  62. {
  63.  
  64. }
  65.  
  66. /* toString - Returns this Animal's name as a string
  67.   * @return name and habitat as String
  68.   */
  69. public String toString()
  70. {
  71. return name + " " + habitat;
  72. }
  73. }
  74.  
  75.  
  76. //////////////////////////////////////////////////////////
  77. /**
  78.  * Species.java
  79.  * Represents a collection of Animals of a similar
  80.  * genotype and classification.
  81.  */
  82. import Animal;
  83.  
  84. public class Species
  85. {
  86. private AnimalNode list;
  87.  
  88. /* constructor - Sets up an initially empty list of Animals */
  89. public Species()
  90. {
  91. list = null;
  92. }
  93.  
  94. /* add - Creates new AnimalNode object, adds it to end of the linked list
  95.   * @param animalName Animal the common name of the animal
  96.   */
  97. public void add(Animal animalName)
  98. {
  99. AnimalNode node = new AnimalNode(animalName);
  100. AnimalNode current;
  101.  
  102. if (list == null)
  103. {
  104. list = node;
  105. }
  106. else
  107. {
  108. current = list;
  109. while (current.next != null)
  110. current = current.next;
  111. current.next = node;
  112. }
  113. }
  114.  
  115. /* toString - returns this list of Animals as a string
  116.   * @return result String outputs animal names line-by-line
  117.   */
  118. public String toString ()
  119. {
  120. String result = "";
  121.  
  122. AnimalNode current = list;
  123.  
  124. while (current != null)
  125. {
  126. result += current.Animal.toString() + "\n";
  127. current = current.next;
  128. }
  129.  
  130. return result;
  131. }
  132.  
  133. /**
  134.   * AnimalNode
  135.   * An inner class that represents a node in the Animal list. The
  136.   * public variables are accessed by the AnimalList class.
  137.   */
  138. private class AnimalNode
  139. {
  140. public Animal Animal;
  141. public AnimalNode next;
  142.  
  143. /* constructor - Sets up the node */
  144. public AnimalNode (Animal theAnimal)
  145. {
  146. Animal = theAnimal;
  147. next = null;
  148. }
  149. }
  150. }
  151.  
  152.  
  153. //////////////////////////////////////////////////////////
  154. /**
  155.  * Zoo.java
  156.  * Driver to exercise the AnimalList collection. There could
  157.  * be many other types of collections of animals, such as a
  158.  * WildernessPreserve, Aquarium, IndiginousRegion, SpeciesGroup, etc
  159.  */
  160. import AnimalList;
  161. import Animal;
  162.  
  163. public class Zoo
  164. {
  165. String zooName = "JAVA"; //name of zoo with default (displayed in output)
  166.  
  167. /*
  168.   * constructor
  169.   * sets up a zoo with a given name
  170.   * @param theZooName String name of the zoo
  171.   */
  172. public Zoo(String theZooName) {
  173. zooName = theZooName;
  174. }
  175.  
  176. /*
  177.   * Creates an AnimalList object, adds several animals to the list,
  178.   * then prints them as a representation of a Zoo.
  179.   */
  180. public static void main (String[] args)
  181. {
  182. AnimalList animals = new AnimalList();
  183.  
  184. //adding all my favorite animals to the Zoo
  185. animals.add (new Animal("Elephant"));
  186. animals.add (new Animal("Tiger"));
  187. animals.add (new Animal("Cheetah"));
  188. animals.add (new Animal("Lion"));
  189. animals.add (new Animal("Hawk"));
  190. animals.add (new Animal("Eagle"));
  191. animals.add (new Animal("Gorilla"));
  192. animals.add (new Animal("Chimpanzee"));
  193. animals.add (new Animal("Spider Monkey"));
  194. animals.add (new Animal("Snow Owl"));
  195. animals.add (new Animal("Otter"));
  196. animals.add (new Animal("Shark"));
  197. animals.add (new Animal("Dolphin"));
  198. animals.add (new Animal("Killer Whale"));
  199.  
  200. System.out.print("'" + zooName + "' Zoo. ");
  201. System.out.println("Proud home to the following animals:");
  202. System.out.println(animals);
  203. }
  204. }

Report this snippet  

You need to login to post a comment.