Java Reference
In-Depth Information
1. Student can add new fields (e.g., gpa ).
2. Student can add new methods (e.g., getGPA ).
3. Student can override existing methods (e.g., toString ).
Two changes are specifically not allowed because they would violate the
notion of an IS-A relationship:
1. Student cannot remove fields.
2. Student cannot remove methods.
Finally, the new class must specify its own constructors; this is likely to
involve some syntax that we will discuss in Section 4.1.6.
Figure 4.3 shows the Student class. The data layout for the Person and
Student classes is shown in Figure 4.4. It illustrates that the memory footprint
of any Student object includes all fields that would be contained in a Person
figure 4.3
Inheritance used to
create Student class
1 class Student extends Person
2 {
3 public Student( String n, int ag, String ad, String p,
4 double g )
5 {
6 /* OOPS! Need some syntax; see Section 4.1.6 */
7 gpa = g; }
8
9 public String toString( )
10 { return getName( ) + " " + getAge( ) + " "
11 + getPhoneNumber( ) + " " + getGPA( ); }
12
13 public double getGPA( )
14 { return gpa; }
15
16 private double gpa;
17 }
figure 4.4
Memory layout with
inheritance. Light
shading indicates
fields that are private,
and accessible only by
methods of the class.
Dark shading in the
Student class
indicates fields that
are not accessible in
the Student class but
are nonetheless
present.
name
address
age
phone
Person Class
name
address
age
phone
Student Class
gpa
Search WWH ::




Custom Search