Source of TestChildren.java


  1: import java.util.Scanner;
  2: import csci2341.Utilities;

  4: /**
  5:  * A program to demonstrate inheritance using Parent and Child classes.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class TestChildren {

 11:     public static void main(String[] args) {

 13:         // Basic Parent (see slide 39)
 14:         Parent p = new Parent();
 15:         p.setValue("Frank");
 16:         System.out.println("Objects in the parent class have a value.");
 17:         System.out.println("That value is returned using "
 18:                 + "the getValue() method.");
 19:         System.out.println("That value is set using "
 20:                 + "the setValue() method.");
 21:         System.out.println("I have set this Parent object's value "
 22:                 + "to \"Frank\".");
 23:         System.out.println();
 24:         System.out.println("Parent's value is: " + p.getValue());
 25:         Utilities.pause();

 27:         // About inheritance
 28:         System.out.println("Classes that extend Parent "
 29:                 + "get the getValue and setValue methods for free.");
 30:         System.out.println("Each of the following children "
 31:                 + "sets its value to \"Fred\".");
 32:         Utilities.pause();

 34:         // Basic Child
 35:         Child1 c1 = new Child1();
 36:         c1.setValue("Fred");
 37:         System.out.println("An object of type Child1 "
 38:                 + "uses the getValue method it inherited.");
 39:         System.out.println();
 40:         c1.sayValue();
 41:         Utilities.pause();

 43:         // Child with getValue that returns "Ba-ba"
 44:         Child2 c2 = new Child2();
 45:         c2.setValue("Fred");
 46:         System.out.println("An object of type Child2 "
 47:                 + "overrides its getValue method "
 48:                 + "to return \"Ba-ba\".");
 49:         System.out.println();
 50:         c2.sayValue();
 51:         Utilities.pause();

 53:         // Child with getValue that changes r to w
 54:         Child3 c3 = new Child3();
 55:         c3.setValue("Fred");
 56:         System.out.println("An object of type Child3 "
 57:                 + "overrides its getValue method "
 58:                 + "to replace any Rs in its value "
 59:                 + "with Ws.");
 60:         System.out.println();
 61:         c3.sayValue();
 62:         Utilities.pause();

 64:         // Inheritance and Polymorphism
 65:         System.out.println("The printValue method expects to be given "
 66:                 + "an object of type Parent.");
 67:         System.out.println("Each Child class extends the Parent class, "
 68:                 + "and so is acceptable as an object of type Parent!");
 69:         System.out.println();
 70:         System.out.println("But even so, each child uses it own getValue method.");
 71:         System.out.println();
 72:         printValue("p", p);
 73:         printValue("c1", c1);
 74:         printValue("c2", c2);
 75:         printValue("c3", c3);
 76:         Utilities.pause();

 78:         // And more
 79:         System.out.println("The doThis method expects a Child1 object.");
 80:         System.out.println("Only a Child1 object will do!");
 81:         System.out.println();
 82:         // doThis(p);   // doesn't work -- expects Child1
 83:         doThis(c1);     // works -- c1 is a Child1
 84:         // doThis(c2);  // doesn't work -- expects Child1
 85:         // doThis(c3);  // doesn't work -- expects Child1
 86:         Utilities.pause();
 87:     }

 89:     /**
 90:      * Print the "variable name" along with the value of that object.
 91:      * Say what type the object is.
 92:      *
 93:      * @param name the "variable name" -- as provided by the caller
 94:      * @param obj the object to call the getValue method on
 95:      */
 96:     public static void printValue(String name, Parent obj) {
 97:         System.out.println("The value of " + name + " is " + obj.getValue());
 98:         System.out.println("  (PS: " + name + " is a " 
 99:             + obj.getClass().getName() + ")");
100:     }

102:     /**
103:      * Report that it's OK to call this method with this object as an argument
104:      *
105:      * @param obj the object that was passed to this method (duh!)
106:      */
107:     public static void doThis(Child1 obj) {
108:         System.out.println("doThis works for a " + obj.getClass().getName());
109:     }

111: }