Java Reference
In-Depth Information
Master
Graduate
Doctorate
Student
Associate
Undergraduate
Object
Person
Bachelor
Staff
Employee
Faculty
figure 7-3
the
Person
class might have a
String
variable called
name
, which all
Person
objects will inherit
whether they are graduate students or faculty employees. The
Student
class might have a
double
variable for their grade point average, which both graduate and undergraduate students inherit, but
employees do not. A graduate student might have a
String
variable to store the title of their thesis.
This would then be inherited by master and doctorate students. At the lowest level of the hierarchy,
a
Doctorate
student might have a
Faculty
instance as their advisor, but with no subclasses, this
will not be inherited by any other class.
the Keyword
super
Another keyword introduced in this chapter is
super
. Super is a way of referring to the superclass.
It's not so different than the
this
keyword, covered earlier in this chapter. It is used in constructors
to invoke the constructor of the superclass, like the
this
keyword was used to invoke another con-
structor of the same class. It is also used to access the variables and methods of the superclass, like
the
this
keyword was used to access the variables of the instance calling the method.
Consider a possible implementation of two of the classes from the
Person
example:
Person
and
Employee
.
Employee
is the subclass of
Person
, and
Person
is the superclass of
Employee
.
public class Person {
private String name;
public Person(String name){
this.setName(name);
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
}
Search WWH ::
Custom Search