Viking Settlers (Subclasses)
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.
- import static java.lang.System.out;
- public class VikingCart {
- // This is the parent class.
- // Subclasses are able to access the fields of
- // this class by using the extends keyword.
- private String name;
- private int wheel;
- private int bed;
- /* The setName method is called
- * a setter method. The setter establishes
- * that name and n share the same value.
- * public is a keyword that does allow access
- * from other classes.
- */
- public void setName(String n) {
- name = n;
- }
- /* The getName method is called
- * a getter method. The getter returns
- * the value of name.
- */
- public String getName() {
- return name;
- }
- public void setWheel(int s) {
- wheel = s;
- }
- public int getWheel() {
- return wheel;
- }
- public void setBed(int h) {
- bed = h;
- }
- public int getBed() {
- return bed;
- }
- }
Here is the code for the first subclass.
- import static java.lang.System.out;
- public class VikingCartWood extends VikingCart {
- private int wheel;
- private int bed;
- private int cord;
- public void setWheel(int w) {
- wheel = w;
- }
- public int getWheel() {
- return wheel;
- }
- public void setBed(int b) {
- bed = b;
- }
- public int getBed() {
- return bed;
- }
- public void setCord(int c) {
- cord = c;
- }
- public int getCord() {
- return cord;
- }
- }
Here is the code for the second subclass.
- import static java.lang.System.out;
- public class VikingCartContainer extends VikingCart {
- private int numberofcontainers;
- public void setContainer(int c) {
- numberofcontainers = c;
- }
- public int getContainer() {
- return numberofcontainers;
- }
- }
Here is the code for the calling class.
- import static java.lang.System.out;
- public class UseVikingCart {
- public static void main(String[] args) {
- VikingCartWood thorVikingCartWood = new VikingCartWood();
- VikingCartWood ingridVikingCartWood = new VikingCartWood();
- thorVikingCartWood.setName("Thor's Cart");
- thorVikingCartWood.setWheel(30);
- thorVikingCartWood.setBed(80);
- thorVikingCartWood.setCord(100);
- ingridVikingCartWood.setName("Ingrid's Cart");
- ingridVikingCartWood.setWheel(40);
- ingridVikingCartWood.setBed(90);
- ingridVikingCartWood.setCord(110);
- VikingCartContainer thorVikingCartContainer = new VikingCartContainer();
- VikingCartContainer ingridVikingCartContainer = new VikingCartContainer();
- thorVikingCartContainer.setContainer(3);
- ingridVikingCartContainer.setContainer(7);
- out.println();
- out.println("Name: " + thorVikingCartWood.getName());
- out.println("Wheel Size: " + thorVikingCartWood.getWheel());
- out.println("Bed Square Feet: " + thorVikingCartWood.getBed());
- out.println("Number of Cords of Wood: " + thorVikingCartWood.getCord());
- out.println("Number of Containers: " + thorVikingCartContainer.getContainer());
- out.println();
- out.println("Name: " + ingridVikingCartWood.getName());
- out.println("Wheel Size: " + ingridVikingCartWood.getWheel());
- out.println("Bed Square Feet: " + ingridVikingCartWood.getBed());
- out.println("Number of Cords of Wood: " + ingridVikingCartWood.getCord());
- out.println("Number of Containers: " + ingridVikingCartContainer.getContainer());
- }
- }
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