Source of MorePeopleTests.java


  1: package inheritance;


  4: import java.util.Scanner;

  6: /**
  7:  * A program to demonstrat various bits of inheritance.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class MorePeopleTests {

 13:     public static void main(String[] args) {
 14:         introduceSelf();

 16:         // create objects to work with
 17:         System.out.println("Creating Person p (Pat) and Student s (Sam).");
 18:         Person p = new Person("Pat");
 19:         Student s = new Student("Sam");

 21:         // work with those objects
 22:         showNormalInheritance(p, s);
 23:         showReplacedInheritance(p, s);
 24:         showRevisedInheritance(p, s);
 25:         showPolymorphism(p, s);
 26:     }

 28:     private static void introduceSelf() {
 29:         System.out.println("\n\n"
 30:                 + "More Inheritance Testing using Person and Student\n"
 31:                 + "-------------------------------------------------\n\n"
 32:                 + "Inheritance is when one class 'extends' another.\n"
 33:                 + "In this example Student extends Person.\n"
 34:                 + "In this program, we look at overriding methods.");
 35:         pause();
 36:     }

 38:     private static void showNormalInheritance(Person p, Student s) {
 39:         // public methods
 40:         System.out.println("\nStudent objects inherit public Person methods.");
 41:         System.out.println("\nPerson p has publicPersonMethod:");
 42:         p.publicPersonMethod();
 43:         System.out.println("\nStudent s has publicPersonMethod:");
 44:         s.publicPersonMethod();
 45:         pause();

 47:         // private methods
 48:         System.out.println("private Person methods can only be called "
 49:                 + "from inside the Person class.");
 50:         System.out.println("\nPerson p has public callingPrivatePersonMethod:");
 51:         p.callingPrivatePersonMethod();
 52:         System.out.println("\nStudent s inherits callingPrivatePersonMethod, "
 53:                 + "and *it* can call\nprivatePersonMethod:");
 54:         s.callingPrivatePersonMethod();
 55:         pause();
 56:     }

 58:     private static void showReplacedInheritance(Person p, Student s) {
 59:         System.out.println("Inherited methods can be replaced.");
 60:         System.out.println("\nPerson p has a replacedMethod:");
 61:         p.replacedMethod();
 62:         System.out.println("\nStudent s has a replacedMethod, too:");
 63:         s.replacedMethod();
 64:         System.out.println("\nThe replaced method should have @Override "
 65:                 + "in the child class.");
 66:         pause();
 67:     }

 69:     private static void showRevisedInheritance(Person p, Student s) {
 70:         System.out.println("Even tho' a method is replaced, "
 71:                 + "the child class can still call the\nparent's version "
 72:                 + "by using super.");
 73:         System.out.println("\nPerson p has a revisedMethod:");
 74:         p.revisedMethod();
 75:         System.out.println("\nStudent s has a revisedMethod, too:");
 76:         s.revisedMethod();
 77:         System.out.println("\nThe revised method should have @Override "
 78:                 + "in the child class.");
 79:         pause();
 80:     }

 82:     private static void showPolymorphism(Person p, Person s) {
 83:         System.out.println("Methods expecting a Person can accept a Student.");
 84:         System.out.println("\nPerson p is a " + p.getClass().getName());
 85:         System.out.println("\nPerson s is a " + s.getClass().getName());
 86:         pause();

 88:         System.out.println("Of course it can still only do Person-y things.");
 89:         System.out.println("\nPerson p's name is " + p.getName());
 90:         System.out.println("\nPerson s's name is " + s.getName());
 91:         pause();

 93:         System.out.println("Unless you use instanceof.");
 94:         if (p instanceof Student) {
 95:             System.out.println("\tp is a Student!");
 96:             Student ps = (Student)p;
 97:             System.out.println("\t\t" + ps.getName() + "'s A-# is " 
 98:                     + ps.A_NUMBER);
 99:         } else {
100:             System.out.println("\tp is NOT a Student!");
101:         }
102:         if (s instanceof Student) {
103:             System.out.println("\ts is a Student!");
104:             Student ss = (Student)s;
105:             System.out.println("\t\t" + ss.getName() + "'s A-# is " 
106:                     + ss.A_NUMBER);
107:         } else {
108:             System.out.println("\ts is NOT a Student!");
109:         }
110:         pause();
111:     }

113:     public static void pause() {
114:         Scanner kbd = new Scanner(System.in);
115:         System.out.print("\nPress enter...");
116:         kbd.nextLine();
117:         System.out.println("\n\n\n\n");
118:     }

120: }