Source of MorePeopleTests.java


  1: package inheritance;

  3: import java.util.Scanner;

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

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

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

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

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

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

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

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

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

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

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

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

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

117: }