Source of MorePeopleTests.java


  1: import java.util.Scanner;

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

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

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

 18:         // work with those objects
 19:         showNormalInheritance(p, s);
 20:         showReplacedInheritance(p, s);
 21:         showRevisedInheritance(p, s);
 22:         showPolymorphism(p, s);
 23:     }

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

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

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

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

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

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

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

 90:         System.out.println("Unless you use instanceof.");
 91:         if (p instanceof Student perStu) {
 92:             System.out.println("\tPerson p is a Student!");
 93:             System.out.println("\t\t" + perStu.getName() + "'s A-# is " 
 94:                     + perStu.A_NUMBER);
 95:         } else {
 96:             System.out.println("\tPerson p is NOT a Student!");
 97:         }
 98:         if (s instanceof Student perStu) {
 99:             System.out.println("\tPerson s is a Student!");
100:             System.out.println("\t\t" + perStu.getName() + "'s A-# is " 
101:                     + perStu.A_NUMBER);
102:         } else {
103:             System.out.println("\tPerson s is NOT a Student!");
104:         }
105:         pause();
106:     }

108:     public static void pause() {
109:         Scanner kbd = new Scanner(System.in);
110:         System.out.print("\nPress enter...");
111:         kbd.nextLine();
112:         System.out.println("\n\n\n\n");
113:     }

115: }