Source of Student.java


  1: package inheritance;


  4: /**
  5:  * A simplified Student class to demonstrate inheritance from Person.
  6:  * This class has public and private methods.
  7:  * Undergrad and GradStudent extend this class.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class Student extends Person {

 13:     public final String A_NUMBER;
 14:     private int grade;

 16:     public static final int MAX_GRADE = 100;
 17:     private static int numStudents = 0;

 19:     public Student(String n) {
 20:         super(n);   // set my name AS A Person
 21:         grade = 0;
 22:         A_NUMBER = nextANumber();
 23:     }

 25:     public void setGrade(int g) {
 26:         if (isValidGrade(g)) {
 27:             this.grade = g;
 28:         }
 29:     }

 31:     public int getGrade() {
 32:         return this.grade;
 33:     }

 35:     public void publicStudentMethod() {
 36:         System.out.println("\tin publicStudentMethod for " + this);
 37:         publicPersonMethod();
 38:         privateStudentMethod();
 39:         System.out.println("\tpublicStudentMethod done");
 40:     }

 42:     private void privateStudentMethod() {
 43:         System.out.println("\tin privateStudentMethod for " + this);
 44:     }

 46:     @Override
 47:     public void replacedMethod() {
 48:         System.out.println("\tin replacedMethod for the Student " + this);
 49:     }

 51:     @Override
 52:     public void revisedMethod() {
 53:         System.out.println("\tin revisedMethod for the Student " + this);
 54:         super.revisedMethod();
 55:         System.out.println("\trevisedMethod for Student done");
 56:     }

 58:     @Override
 59:     public String toString() {
 60:         return this.getName() + " (" + this.A_NUMBER + ")";
 61:     }

 63:     private boolean isValidGrade(int g) {
 64:         return 0 <= g && g <= MAX_GRADE;
 65:     }

 67:     private String nextANumber() {
 68:         ++numStudents;
 69:         return String.format("A%08d", numStudents);
 70:     }

 72: }