Savitch 8.1-8.2
Person Class
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
public class Person {
private String name;
public Person() {
name = "No name yet";
}
public Person(String initialName) {
name = initialName;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public void writeOutput() {
System.out.println("Name: " + name);
}
public boolean hasSameName(Person otherPerson) {
return name.equalsIgnoreCase(otherPerson.name);
}
}
extends keyword.
1
public class Students extends Person
Student Class
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
public class Student extends Person {
private int studentNumber;
public Student() {
super();
studentNumber = 0;
}
public Student(String initialName, int initialStudentNumber) {
super(initialName);
studentNumber = initialNumber;
}
public void reset(String newName, int newStudentNumber) {
setName(newName);
studentNumber = newStudentNumber;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int newStudentNumber) {
studentNumber = newStudentNumber;
}
public void writeOutput() {
System.out.println("Name: " + getName());
System.out.println("Student Number: " + studentNumber);
}
public boolean equals(Student otherStudent) {
return this.hasSameName(otherStudent) &&
(this.studentNumber == otherStudent.studentNumber);
}
}
Person class and the Student class can be called on an object of the Student class.
1
2
3
4
5
6
7
8
9
10
11
public class Inheritance Demo {
public static void main(String[] args) {
Student student = new Student();
student.setName("Warren Peace"); // Person method
student.setStudentNumber(1234); // Student method
student.writeOutput(); // ???
}
}
Person class and the Student class both have methods called writeOutput that have no parameters.writeOutput in the derived class Student overrides the definition in the base class Person.writeOutput is called on a Student object it will use the definition in the Student class.final.
1
public final void specialMethod();
1
2
3
4
public void reset(String newName, int newStudentNumber) {
name = newName; // ILLEGAL!
studentNumber = newStudentNumber;
}
super.
1
2
3
4
public Student(String initialName, int initialStudentNumber) {
super(initialName); // calls Person(String initialName)
studentNumber = initialNumber;
}
super must be the first statement in the constructor.this Method – Againthis can be used to call a constructor in the same class.
1
2
3
4
5
6
7
public Person(String initialName) {
name = initialName;
}
public Person() {
this("No Name Yet"); // calls Person(String initialName)
}
super, a call to this must be the first statement in the constructor.super and this
super keyword can also be used to call a base class method that has been overridden in the derived.
1
2
3
4
5
6
7
8
9
10
// Person class version
public void writeOutput() {
System.out.println("Name: " + name);
}
// Student class version calls Person class version
public void writeOutput() {
super.writeOutput();
System.out.println("Student Number: " + studentNumber);
}
1
public class Undergraduate extends Student
Undergraduate class inherits the public methods of Student along with the public methods that Student inherited from Person.
1
2
3
4
Undergraduate undergrad = new Undergraduate();
undergrad.setName("Anna Karenina"); // Person method
undergrad.setStudentNumber("1878"); // Student method
undergrad.setLevel(2); // Undergraduate method
Undergraduate Class
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
public class Undergraduate extends Student {
private int level; // 1 for freshman, 2 for sophomore
// 3 for junior, or 4 for senior
public Undergraduate() {
super();
level = 1;
}
public Undergraduate(String initialName, int initialStudentNumber,
int initialLevel) {
super(initialName, initialStudentNumber);
setLevel(intialLevel); // checks 1 <= initialLevel <= 4
}
public getLevel() {
return level;
}
public setLevel(int newLevel) {
if ((1 <= newLevel) && (newLevel <= 4))
level = newLevel
else {
System.out.println("Illegal level!");
System.exit(0);
}
}
public void reset(String newName, int newStudentNumber, int newLevel) {
reset(newName, newStudentNumber);
setLevel(newLevel); // Checks 1 <= newLevel <= 4;
}
public void writeOutput()
{
super.writeOutput();
System.out.println("StudentLevel: " + level);
}
public boolean equals(Undergraduate otherUG) {
return equals((Student)otherUG) && (this.level == otherUG.level);
}
}
equals methodequals method in the Undergraduate class uses a cast to make sure it calls the Students version of equals, rather than itself.
1
2
3
4
public boolean equals(Undergraduate otherUG) {
return equals((Student)otherUG) &&
(this.level == otherUG.level);
}
Student version of equals could be called by using the super keyword.
1
2
3
4
public boolean equals(Undergraduate otherUG) {
return super.equals(otherUG) &&
(this.level == otherUG.level);
}
1
2
3
4
5
6
7
8
9
Person person = new Person("Lita Ford");
Student student = new Student("Joan Jett", "1234");
Undergraduate undergrad = new Undergraduate("Cherie Currie", "5678", 3);
if(person.hasSameName(student)) // A Student is a Person
System.out.println("Same Name!");
if(undergrad.hasSameName(person)) // An Undergrad is a Person
System.out.println("Same Name!");
1
2
3
Person p1 = new Student(); // valid
Person p2 = new Undergraduate(); // valid
Student s1 = new Undergraduate(); // valid
1
2
3
Student s = new Person(); // ILLEGAL!
Undergraduate ug1 = new Person(); // ILLEGAL!
Undergraduate ug2 = new Student(); // ILLEGAL!
Object
Object class, the ancestor of all Java classes.Objectclass contains methods that are inherited or overridden by all other classes.
1
2
public String toString()
public boolean equals(Object otherObject)
toString method, an object of that class can be passed to the System.out.println()method:
1
2
3
Person funkyPerson = new Person("Bootsy Collins");
System.out.println(funkyPerson); // calls toString method of Person class
System.out.println(funkyPerson.toString()); // same as above
equals MethodObject.
1
2
3
4
5
public boolean equals(Object otherObject) {
Student otherStudent = (Student)otherObject; // cast to Student
return this.hasSameName(otherStudent) &&
this.studentNumber == otherStudent.studentNumber;
}
null or its class is not derived from Student.equals Method
1
2
3
4
5
6
7
8
9
10
11
public boolean equals(Object otherObject)
{
boolean isEqual = false;
if ((otherObject != null) &&
(otherObject instanceof Student)) {
Student otherStudent = (Student)otherObject;
isEqual = this.sameName(otherStudent) &&
(this.studentNumber == otherStudent.studentNumber);
}
return isEqual;
}