1 2 3 /* Demo for: Inheritance

advertisement
1 /* Demo for: Inheritance
2
Undergrad.java
3
This is a template for undergraduate student objects. It is a subclass
4
of the Student class */
5
6 public class Undergrad 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
String level;
12
int sat;
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 Undergrad(String i, String n, char g, float gpa,
23
String l, int s)
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
level = l;
30
sat = s;
31
}
32
33
// This method overRIDEs retrieveInfo() in the superclass with
34
// an identical method header. It first calls the version in the
35
// superclass to generate part of its output. Then it adds its
36
// own part of the output, which deals with the info specific to
37
// this subclass.
38
public void retrieveInfo()
39
{
40
// Since the retrieveInfo() method in the superclass gets
41
// the values of superclass-defined variables, we may save
42
// some typing by simply calling the superclass method to
43
// obtain those variables' values (compare this with how we
44
// achieve the same goal in the retrieveInfo() method in the
45
// Graduate subclass).
46
super.retrieveInfo();
47
E:/BCIS 3680/07a-inheritance/src/Undergrad.java
1 of 2
48
49
50
51
52
53
54
55
56
57
58
59
60
61 }
// Staring the subclass-specific output
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("Info Specific to Undergrad Subclass");
// 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
String info = "Level: " + level;
info += "\nSAT Score: " + sat;
System.out.println(info);
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
}
E:/BCIS 3680/07a-inheritance/src/Undergrad.java
2 of 2
Download