1 /* Demo for: Inheritance 2 Graduate.java

advertisement
1 /* Demo for: Inheritance
2
Graduate.java
3
This is a template for graduate student objects. It is a subclass
4
of the Student class */
5
6 public class Graduate extends Student
7 {
8
// Instance variables specific to this subclass
9
// For ease of reading, getters and setters for these variables
10
// are omitted.
11
boolean doctoral;
12
int gmat;
13
14
// This is the non-default contructor
15
// It first calls the constructor in the superclass (Student)
16
// to initialize those variables that are defined in the
17
// superclass. It then initializes those variables that are
18
// defined in this subclass.
19
// Note that the parameter list includes all pieces of
20
// information, for both those superclass-defined variables
21
// and subclass-defined variables.
22
public Graduate (String i, String n, char g, float gpa,
23
boolean d, int gmat)
24
{
25
// Call the superclass constructor by passing input it needs
26
super(i, n, g, gpa);
27
28
// Initializes variables defined in this subclass
29
doctoral = d;
30
this.gmat = gmat;
31
}
32
33
// Override the superclass version. Unlike its counterpart in the
34
// Undergrad class, this version doesn't call the superclass version
35
// to generate part of its output. Everything is done from scratch.
36
public void retrieveInfo()
37
{
38
// Notice that the superclass-defined variables are
39
// accessible here by calling their getter methods
40
String infoStr = "\nUNT ID: " + this.getUNTID();
41
infoStr += "\nName: " + this.getName();
42
infoStr += "\nGender: " + this.getGender();
E:/BCIS 3680/07a-inheritance/src/Graduate.java
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 }
infoStr += "\nGPA: " + this.getGPA();
//
//
//
if
{
Now, retrieve values of those variables defined in this class
They are directly accessible (no need to call getter methods
because they were defined earlier in this class
( doctoral )
infoStr += "\nDoctoral Student";
}
else
{
infoStr += "\nMaster's Student";
}
infoStr += "\nGMAT Score: " + gmat;
System.out.println("***********************************");
System.out.println("Running Version in Graduate Subclass");
System.out.println("Very different from Student Version");
System.out.println(infoStr);
System.out.println("************************************\n");
}
E:/BCIS 3680/07a-inheritance/src/Graduate.java
Download