1 2 3 4

advertisement
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
Undergrad.java
The only difference between this version of the Undergrad
class and the previous one is that it implements the abstract
method retrieveInfo() defined in its superclass. In contrast,
in the examples without abstract methods, the retrieveInfo()
method overrode the concrete method retrieveInfo() in the
Student class.
*/
public class Graduate extends Student
{
// Instance variables specific to this subclass
// For ease of reading, getters and setters for these variables
// are omitted.
boolean doctoral;
int gmat;
// This is the non-default contructor
// It first calls the constructor in the superclass (Student)
// to initialize those variables that are defined in the
// superclass. It then initializes those variables that are
// defined in this subclass.
// Note that the parameter list includes all pieces of
// information, for both those superclass-defined variables
// and subclass-defined variables.
public Graduate (String i, String n, char g, float gpa,
boolean d, int gmat)
{
// Call the superclass constructor by passing input it needs
super(i, n, g, gpa);
// Initializes variables defined in this subclass
doctoral = d;
this.gmat = gmat;
}
// Note how this implementation of the retrieveInfo()
// abstract method differs from that in the Undergrad
// class.
public void retrieveInfo()
{
// Note the use of getter methods
String infoStr = "\nUNT ID: " + this.getUNTID();
infoStr += "\nName: " + this.getName();
infoStr += "\nGender: " + this.getGender();
infoStr += "\nGPA: " + this.getGPA();
E:/BCIS 3680/07b-abstract/src/Graduate.java
1 of 2
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 }
// Now, retrieve values of those variables defined in this
// subclass
infoStr += "\nDoctoral: ";
if ( doctoral )
{
infoStr += "Yes";
}
else
{
infoStr += "No";
}
infoStr += ("\nGMAT Score: " + gmat);
System.out.println("***********************************");
System.out.println("
Implementing Abstract Method");
System.out.println(infoStr);
System.out.println("***********************************\n");
}
E:/BCIS 3680/07b-abstract/src/Graduate.java
2 of 2
Download