1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 /* Demo for: Abstract Methods Student.java The only difference between this version of the Student class and the previous one is that now the retrieveInfo() method is defined as an abstract method. Also note the "abstrct" keyword in the class header. */ public abstract class Student { //***** Static Variable *****// static int studentCount; //***** private private private private Instance Variables *****// String untId; String name; char gender; float gpa; // ***** Constructor *****// // It doesn't make sense to accept the default initial // values for these instance variables because each student // would have the name "null", ID "null" and is of the ' ' // gender. Therefore, a non-default constructor method is // created below, instead of using the default constructor // that Java runs implicitly for classes without an explicitly // created constructor method. public Student(String i, String n, char g, float gpa) { untId = i; name = n; gender = g; this.gpa = gpa; } // Like regular methods, a constructor method can be overloaded. // If warranted by software specs, we can create multiple version of the // constructor method. Below, we create a version that emulates the // "default" or implicit constructor that Java will run if no construtor // method is defined. (As an aside, if a constructor is created, Java will // assume that we want to be control and will not run the "default" // constructor any more.) public Student() { } // Another version of the constructor. This version only initializes the ID E:/BCIS 3680/07b-abstract/src/Student.java 1 of 3 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 // and name. A realistic business scenario may be that some demographic data // of the customers are optional. So it may be that the only mandatory fields // of input are ID and name. In this case, we only initialize ID and name. public Student(String i, String n) { untId = i; name = n; } // ***** Setters and Getters *****// // For UNT ID // public void setUNTID(String id) { untId = id; } public String getUNTID() { return untId; } // For Name // public void setName(String name) { this.name = name; } public String getName() { return name; } // For Gender // public void setGender(char gender) { this.gender = gender; } public char getGender() { return gender; } // For Age // public void setGPA(float gpa) { E:/BCIS 3680/07b-abstract/src/Student.java 2 of 3 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 } this.gpa = gpa; } public float getGPA() { return gpa; } // ***** Static Methods *****// static void addToCount() { studentCount++; } static int readStudentCount() { return studentCount; } // ***** Abstract Method *****// public abstract void retrieveInfo(); E:/BCIS 3680/07b-abstract/src/Student.java 3 of 3