The document discusses inheritance and interacting with the superclass in Java. It shows examples of extending a Teacher class to create a SeniorTeacher class. The key lessons are:
1) When overriding a method in the subclass, you need to use the super keyword to call the superclass implementation, otherwise it will be shadowed.
2) Private fields and methods can only be accessed by the class itself, not subclasses, so the subclass needs to call public methods instead of trying to access private fields directly.
3) When extending a class, the subclass must call the superclass constructor using super before accessing fields or doing other initialization.
2. super
public class Teacher {
public int getSickDays() {
return 15;
}
public String getGreeting() {
return "Welcome to class!";
}
}
public class SeniorTeacher extends Teacher {
public int getSickDays() {
return 20; // bad implementation
}
}
2
Sunday, October 27, 13
3. super
public class Teacher {
public int getSickDays() {
return 15;
}
public String getGreeting() {
return "Welcome to class!";
}
}
public class SeniorTeacher extends Teacher {
public int getSickDays() {
return getSickDays() + 5; // does this work?
}
}
3
Sunday, October 27, 13
4. super
public class SeniorTeacher extends Teacher {
public int getSickDays() {
return getSickDays() + 5;
}
}
Why doesn’t this work?
a. getSickDays() isn’t a variable
b. getSickDays() is shadowed implicitly
c. This solution works
4
Sunday, October 27, 13
5. super
public class Teacher {
public int getSickDays() {
return 15;
}
public String getGreeting() {
return "Welcome to class!";
}
}
public class SeniorTeacher extends Teacher {
public int getSickDays() {
return super.getSickDays() + 5; // good!
}
}
5
Sunday, October 27, 13
6. private access
public class Teacher {
private int sickDays;
public Teacher() {
sickDays = 15;
}
public int getSickDays() {
return sickDays;
}
}
6
Sunday, October 27, 13
7. private access
public class Teacher {
private int sickDays;
public Teacher() {
sickDays = 15;
}
public int getSickDays() {
return sickDays;
}
}
public class SeniorTeacher {
public int getSickDays() {
return sickDays + 5; // private access!
}
}
Sunday, October 27, 13
7
8. private access
public class Teacher {
private int sickDays;
public Teacher() {
sickDays = 15;
}
public int getSickDays() {
return sickDays;
}
}
public class SeniorTeacher {
public int getSickDays() {
return super.getSickDays() + 5; // public method access
}
}
8
Sunday, October 27, 13
9. Superclass constructor
public class Teacher {
private int sickDays;
public Teacher(int days) {
sickDays = days;
}
public int getSickDays() { ... }
}
public class SeniorTeacher {
private int yearsTeaching;
public SeniorTeacher(int days, int years) {
sickDays = days + (2 * years); // private access!
yearsTeaching = years;
}
}
Sunday, October 27, 13
9
10. Superclass constructor
public class Teacher {
private int sickDays;
public Teacher(int days) {
sickDays = days;
}
public int getSickDays() { ... }
}
public class SeniorTeacher {
private int yearsTeaching;
public SeniorTeacher(int days, int years) {
super(days + (2 * years)); // superclass constructor
yearsTeaching = years;
}
}
Sunday, October 27, 13
10