Unit 9 Inheritance
Unit 9 Inheritance
Notes
The keyword "Super" is used to call a subclass.
Overriding methods (@Override) is used to give different implementations to the method of the superclass
Subclasses and Superclasses are usually organized into a single root tree caled a hierarchy
One example includes: the Class leaf extends nature, so leaf is a subclass of nature.
public class Team {
public int score;
public int wins;
public int all_goals;
public Team( int score, int wins, int all_goals) {
this.score = score;
this.wins = wins;
this.all_goals = all_goals;
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Unknown");
}
public void out() {
System.out.println("Team score: " + this.score);
System.out.println("Team wins: " + this.wins);
System.out.println("Team all goals: " + this.all_goals);
bestplayer();
}
}
public class Portugal extends Team {
public Portugal(int score, int wins, int all_goals) {
super(score, wins, all_goals);
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Cristiano Ronaldo");
}
}
public class France extends Team {
public France(int score, int wins, int all_goals) {
super(score, wins, all_goals);
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Kylian Mbappe");
}
}
Team team = new Team(0, 1, 27);
Team portugal = new Portugal(0, 2, 55);
Team france = new France(0, 2, 129);
team.out();
portugal.out();
france.out();
public class Person {
protected String name;
protected String birthday;
public Person (String name, String birthday){
this.name = name;
this.birthday = birthday;
}
public String getName(){
return name;
}
public int getAge() {
return 2022 - Integer.parseInt(birthday);
}
@Override
public String toString() {
return "Person (name: " + name + ", birthday: " + birthday + ")";
}
}
//Student is a subclass and extends person, the superclass, since students are classified as a person.
public class Student extends Person {
private int grade;
private double gpa;
private String extracurricular;
public Student (String name, String birthday, int grade, double gpa, String extracurricular) {
super(name, birthday);
this.grade = grade;
this.gpa = gpa;
this.extracurricular = extracurricular;
}
// return gpa
public double getGPA() {
return gpa;
}
public String extracurricular() {
return extracurricular;
}
// return grade
public int getGrade(){
return grade;
}
@Override
public String toString() {
return "Student (name: " + name + ", birthday: " + birthday + ", extracurricular: " + extracurricular + ", gpa:" + gpa + ", grade: " + grade + ")";
}
}
// The public class teacher is a subclass of person and as such will extend person, the superclass.
public class Teacher extends Person {
private String subject;
private int tenureYears;
private String degree;
public Teacher (String name, String birthday, String subject, int tenureYears, String degree) {
super(name, birthday);
this.subject = subject;
this.tenureYears = tenureYears;
this.degree = degree;
}
// return subject
public String getSubject() {
return subject;
}
// return yearsOfExperience
public int getTenure() {
return tenureYears;
}
// return degree
public String getDegree() {
return degree;
}
@Override
public String toString() {
return "Teacher (name: " + name + ", birthday: " + birthday + ", subject: " + subject + ", tenureYears:" + tenureYears + ", degree: " + degree + ")";
}
}
Person aidan = new Person("aidan", "2006");
System.out.println(aidan.toString());
Person jun = new Student("jun", "2005", 12, 3.5, "football");
System.out.println(jun.toString());
Person allie = new Teacher("allie", "1980", "math", 10, "N/A");
System.out.println(allie.toString());