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 Undergrad extends Student
{
// Instance variables specific to this subclass
// For ease of reading, getters and setters for these variables
// are omitted.
String level;
int sat;
// 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 Undergrad(String i, String n, char g, float gpa,
String l, int s)
{
// Call the superclass constructor by passing input it needs
super(i, n, g, gpa);
// Initializes variables defined in this subclass
level = l;
sat = s;
}
// This method IMPLEMENTS the retrieveInfo() abstract
// method in the superclass. Notice that the behavior
// of this method differs quite a bit from that of
// the implementation of the same abstract method in
// the Graduate class. The info here is expressed as
// sentences, whereas in the Graduate class it is
// presented in the format of "Attribute: Value".
public void retrieveInfo()
{
// Determine which possessive pronoun to use
E:/BCIS 3680/07b-abstract/src/Undergrad.java
1 of 2
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 }
// based on student's gender
String pronoun = "";
if ( this.getGender() == 'M')
{
pronoun += "His";
}
else
{
pronoun += "Her";
}
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("
Implementing Abstract Method");
// String to contain info
String info = "\nUndergrad Info:";
info += "\n" + this.getName() + " is a " + level + ". ";
info += pronoun + " GPA is " + this.getGPA() + ".\n";
info += pronoun + " SAT was " + sat + ".";
System.out.println(info);
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
}
E:/BCIS 3680/07b-abstract/src/Undergrad.java
2 of 2
Download