Skip to content

Activity 10: Inheritance

Instructions

Example Code

Person

 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
import java.time.LocalDate;

public class Person {

    protected String name;
    protected LocalDate birth;

    public Person(String name, LocalDate birth) {
        this.name = name;
        this.birth = birth;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Person) {
            Person other = (Person) obj;
            return name.equals(other.name) && birth.equals(other.birth);
        }
        return false;
    }

    public LocalDate getBirth() {
        return birth;
    }

    public String getName() {
        return name;
    }

    public void setBirth(LocalDate birth) {
        this.birth = birth;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return String.format("%s (%s)", name, birth);
    }

}

Student

 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
43
44
45
46
47
48
49
import java.time.LocalDate;

public class Student extends Person {

    private static int nextId = 1;

    protected int sid;     // student id number
    protected boolean ta;  // teaching assistant?

    public Student(String name, LocalDate birth) {
        super(name, birth);
        this.sid = nextId++;
        this.ta = false;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Student) {
            Student other = (Student) obj;
            return sid == other.sid;
        }
        return super.equals(obj);
    }

    public int getSid() {
        return sid;
    }

    public boolean isTA() {
        return ta;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public void setTA(boolean ta) {
        this.ta = ta;
    }

    public String toString() {
        String str = super.toString();
        str += " [sid: " + sid + "]";
        if (ta) {
            str += " TA";
        }
        return str;
    }

}

Teacher

 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
import java.time.LocalDate;

public class Teacher extends Person {

    protected LocalDate hired;
    protected int salary;

    public Teacher(String name, LocalDate birth, int salary) {
        super(name, birth);
        this.hired = LocalDate.now();
        this.salary = salary;
    }

    public LocalDate getHired() {
        return hired;
    }

    public int getSalary() {
        return salary;
    }

    public void raise(double percent) {
        salary += Math.round(salary * percent);
    }

    public String toString() {
        return String.format("%s hired on %s making $%,d",
                super.toString(), hired, salary);
    }

}

Model 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.time.LocalDate;

public class Model1 {

    public static void main(String[] args) {
        Person p = new Person("Alan", LocalDate.parse("1912-06-23"));
        Student s = new Student("Grace", LocalDate.parse("1906-12-09"));
        Teacher t = new Teacher("John", LocalDate.parse("1903-12-28"), 123456);

        System.out.println(p);
        System.out.println(s);
        System.out.println(t);

        // Question 8
        System.out.println(p.equals(p));
        System.out.println(s.equals(s));
        System.out.println(t.equals(t));
    }

}

Model 2

 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
import java.time.LocalDate;

public class Model2 {

    public static void main(String[] args) {
        Person p1 = new Person("Helen", LocalDate.parse("2000-01-02"));
        Student s1 = new Student("John", LocalDate.parse("2000-03-04"));
        Person poly = new Student("Polly", LocalDate.parse("2000-05-06"));

        System.out.println(p1 instanceof Student);
        System.out.println(s1 instanceof Student);
        System.out.println(poly instanceof Student);

        // Question 18
        LocalDate d = LocalDate.parse("1949-01-17");
        Object obj = new Teacher("Anita Borg", d, 123456);
        System.out.println(obj.toString());

        // Question 19
        Person j = new Student("John", LocalDate.parse("2000-03-04"));
        Person m = new Teacher("Mary", LocalDate.parse("2000-09-10"), 100000);
        System.out.println(j.equals(m));
        System.out.println(m.equals(j));
    }

}