Viking Settlers (Subclasses)

classes diagram

From the 800's to 1066 the Anglo-Saxons and the Vikings (primarily Danes) would fight for control of Britain. There would be Anglo-Saxon kings and Viking kings. Eventually the people of Britain started to think of themselves as English. For farming and trade the Vikings developed multiple cart types. Do a search on Viking carts and you will see some interesting vehicles. For this training module you will build a subclass for a wood cart, and another subclass for a cart that hauls containers.

The diagram to the right shows the different files that you will make in order to demonstrate Java's use of subclasses.

Here is the code for a class that establishes fields in a parent cart class.

Java code

  1. import static java.lang.System.out;
  2. public class VikingCart {
  3. // This is the parent class.
  4. // Subclasses are able to access the fields of
  5. // this class by using the extends keyword.
  6. private String name;
  7. private int wheel;
  8. private int bed;
  9. /* The setName method is called
  10. * a setter method. The setter establishes
  11. * that name and n share the same value.
  12. * public is a keyword that does allow access
  13. * from other classes.
  14. */
  15. public void setName(String n) {
  16. name = n;
  17. }
  18. /* The getName method is called
  19. * a getter method. The getter returns
  20. * the value of name.
  21. */
  22. public String getName() {
  23. return name;
  24. }
  25. public void setWheel(int s) {
  26. wheel = s;
  27. }
  28. public int getWheel() {
  29. return wheel;
  30. }
  31. public void setBed(int h) {
  32. bed = h;
  33. }
  34. public int getBed() {
  35. return bed;
  36. }
  37. }

 

Here is the code for the first subclass.

Java code

  1. import static java.lang.System.out;
  2. public class VikingCartWood extends VikingCart {
  3. private int wheel;
  4. private int bed;
  5. private int cord;
  6. public void setWheel(int w) {
  7. wheel = w;
  8. }
  9. public int getWheel() {
  10. return wheel;
  11. }
  12. public void setBed(int b) {
  13. bed = b;
  14. }
  15. public int getBed() {
  16. return bed;
  17. }
  18. public void setCord(int c) {
  19. cord = c;
  20. }
  21. public int getCord() {
  22. return cord;
  23. }
  24. }

Here is the code for the second subclass.

Java code

  1. import static java.lang.System.out;
  2. public class VikingCartContainer extends VikingCart {
  3. private int numberofcontainers;
  4. public void setContainer(int c) {
  5. numberofcontainers = c;
  6. }
  7. public int getContainer() {
  8. return numberofcontainers;
  9. }
  10. }

Here is the code for the calling class.

  1. import static java.lang.System.out;
  2. public class UseVikingCart {
  3. public static void main(String[] args) {
  4. VikingCartWood thorVikingCartWood = new VikingCartWood();
  5. VikingCartWood ingridVikingCartWood = new VikingCartWood();
  6. thorVikingCartWood.setName("Thor's Cart");
  7. thorVikingCartWood.setWheel(30);
  8. thorVikingCartWood.setBed(80);
  9. thorVikingCartWood.setCord(100);
  10. ingridVikingCartWood.setName("Ingrid's Cart");
  11. ingridVikingCartWood.setWheel(40);
  12. ingridVikingCartWood.setBed(90);
  13. ingridVikingCartWood.setCord(110);
  14. VikingCartContainer thorVikingCartContainer = new VikingCartContainer();
  15. VikingCartContainer ingridVikingCartContainer = new VikingCartContainer();
  16. thorVikingCartContainer.setContainer(3);
  17. ingridVikingCartContainer.setContainer(7);
  18. out.println();
  19. out.println("Name: " + thorVikingCartWood.getName());
  20. out.println("Wheel Size: " + thorVikingCartWood.getWheel());
  21. out.println("Bed Square Feet: " + thorVikingCartWood.getBed());
  22. out.println("Number of Cords of Wood: " + thorVikingCartWood.getCord());
  23. out.println("Number of Containers: " + thorVikingCartContainer.getContainer());
  24. out.println();
  25. out.println("Name: " + ingridVikingCartWood.getName());
  26. out.println("Wheel Size: " + ingridVikingCartWood.getWheel());
  27. out.println("Bed Square Feet: " + ingridVikingCartWood.getBed());
  28. out.println("Number of Cords of Wood: " + ingridVikingCartWood.getCord());
  29. out.println("Number of Containers: " + ingridVikingCartContainer.getContainer());
  30. }
  31. }

Java code

Use Save As to save the code files to your folder. Then compile the code files in the Command prompt window. Run the UseVikingCart.java class.

Your output should look like this:

c:\javamike>java UseVikingCart

Name: Thor's Cart

Wheel Size: 30

Bed Square Feet: 80

Number of Cords of Wood: 100

Number of Containers: 3

 

Name: Ingrid's Cart

Wheel Size: 40

Bed Square Feet: 90

Number of Cords of Wood: 110

Number of Containers: 7

 

< Vikings (Accessors) Normans (Constructors) >