public class MorePeopleTests
2: import java.util.Scanner;
4: /**
5: * A program to demonstrat various bits of inheritance.
6: *
7: * @author Mark Young (A00000000)
8: */
9: public class MorePeopleTests {
11: public static void main(String[] args) {
12: introduceSelf();
14: // create objects to work with
15: System.out.println("Creating Person p (Pat) and Student s (Sam).");
16: Person p = new Person("Pat");
17: Student s = new Student("Sam");
19: // work with those objects
20: showNormalInheritance(p, s);
21: showReplacedInheritance(p, s);
22: showRevisedInheritance(p, s);
23: showPolymorphism(p, s);
24: }
26: /**
27: * Introduce this program.
28: */
29: private static void introduceSelf() {
30: System.out.println("\n\n"
31: + "More Inheritance Testing using Person and Student\n"
32: + "-------------------------------------------------\n\n"
33: + "Inheritance is when one class 'extends' another.\n"
34: + "In this example Student extends Person.\n"
35: + "In this program, we look at overriding methods.");
36: pause();
37: }
39: /**
40: * Demonstrate inheriting methods.
41: *
42: * @param p just some Person
43: * @param s just some Student
44: */
45: private static void showNormalInheritance(Person p, Student s) {
46: // public methods
47: System.out.println("\nStudent objects inherit public Person methods.");
48: System.out.println("\nPerson p has publicPersonMethod:");
49: p.publicPersonMethod();
50: System.out.println("\nStudent s has publicPersonMethod:");
51: s.publicPersonMethod();
52: pause();
54: // private methods
55: System.out.println("private Person methods can only be called "
56: + "from inside the Person class.");
57: System.out.println("\nPerson p has public callingPrivatePersonMethod:");
58: p.callingPrivatePersonMethod();
59: System.out.println("\nStudent s inherits callingPrivatePersonMethod, "
60: + "and *it* can call\nprivatePersonMethod:");
61: s.callingPrivatePersonMethod();
62: pause();
63: }
65: /**
66: * Demonstrate replacing an inherited method.
67: *
68: * @param p just some Person
69: * @param s just some Student
70: */
71: private static void showReplacedInheritance(Person p, Student s) {
72: System.out.println("Inherited methods can be replaced.");
73: System.out.println("\nPerson p has a replacedMethod:");
74: p.replacedMethod();
75: System.out.println("\nStudent s has a replacedMethod, too:");
76: s.replacedMethod();
77: System.out.println("\nThe replaced method should have @Override "
78: + "in the child class.");
79: pause();
80: }
82: /**
83: * Demonstrate "revising" an inherited method.
84: *
85: * @param p just some Person
86: * @param s just some Student
87: */
88: private static void showRevisedInheritance(Person p, Student s) {
89: System.out.println("Even tho' a method is replaced, "
90: + "the child class can still call the\nparent's version "
91: + "by using super.");
92: System.out.println("\nPerson p has a revisedMethod:");
93: p.revisedMethod();
94: System.out.println("\nStudent s has a revisedMethod, too:");
95: s.revisedMethod();
96: System.out.println("\nThe revised method should have @Override "
97: + "in the child class.");
98: pause();
99: }
101: /**
102: * Demonstrate polymorhism.
103: *
104: * @param p just some Person
105: * @param s just some other Person, who happens to be a Student
106: */
107: private static void showPolymorphism(Person p, Person s) {
108: System.out.println("Methods expecting a Person can accept a Student.");
109: System.out.println("\nPerson p is a " + p.getClass().getName());
110: System.out.println("\nPerson s is a " + s.getClass().getName());
111: pause();
113: System.out.println("Of course it can still only do Person-y things.");
114: System.out.println("\nPerson p's name is " + p.getName());
115: System.out.println("\nPerson s's name is " + s.getName());
116: pause();
118: System.out.println("Unless you use instanceof.");
119: if (p instanceof Student) {
120: System.out.println("\tp is a Student!");
121: Student ps = (Student)p;
122: System.out.println("\t\t" + ps.getName() + "'s A-# is "
123: + ps.A_NUMBER);
124: } else {
125: System.out.println("\tp is NOT a Student!");
126: }
127: if (s instanceof Student) {
128: System.out.println("\ts is a Student!");
129: Student ss = (Student)s;
130: System.out.println("\t\t" + ss.getName() + "'s A-# is "
131: + ss.A_NUMBER);
132: } else {
133: System.out.println("\ts is NOT a Student!");
134: }
135: pause();
136: }
138: /**
139: * Prompt the user and wait for them to press the enter key.
140: */
141: public static void pause() {
142: Scanner kbd = new Scanner(System.in);
143: System.out.print("\nPress enter...");
144: kbd.nextLine();
145: System.out.println("\n\n\n\n");
146: }
148: }