Unit 9 - Inheritance
CB Unit 9
Inheritance
Capability of a class to derive characeristics from another class.
Superclass/Subclass
-
Superclass: The class from which a subclass is derived
public class A
-
Subclass: The class that derives from another class
public class B extends A
-
Example: Class truck extends car, so truck is a subclass of car.
- Allows objects to use methods and atttributes of the object it is extending.
- Subclass can be extended by another subclass, it then technically becomes a superclass too
Constructors
-
super
keyword is used to refer to the superclass - Used when initializing in a subclass
Methods
- Subclass can override methods in superclass
-
@Override
annotation is used to indicate that a method is overriding a superclass method- Commonly used to override toString method
References / Polymorphism
-
Fruit fruit1 = new Apple()
- Object and reference type can be different -
Fruit fruit2 = new Fruit()
- Object and reference type are the same - Polymorphism is useful for having a method perform different tasks
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");
}
}
public class Morocco extends Team {
public Morocco(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: Youssef En-Nesyri");
}
}
public class Netherlands extends Team {
public Netherlands(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: Memphis Depay");
}
}
public class Croatia extends Team {
public Croatia(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: Luka Modric");
}
}
Team team = new Team(0, 1, 27);
Team portugal = new Portugal(0, 2, 55);
Team france = new France(0, 2, 129);
Team morocco = new Morocco(0, 1, 18);
Team netherlands = new Netherlands(0, 1, 94);
Team croatia = new Croatia(0, 1, 40);
team.out();
portugal.out();
france.out();
morocco.out();
netherlands.out();
croatia.out();
Person/student subclass homework
- Add a getAge method in the Person super class
- Create a new subclass Student with additional members of your choice to personalize the Student class
- Create a new subclass Teacher with additiona members of your choice
- Override the toString method using the @Override to print a Student and teacher object with new members
- Print the student and teacher.
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 + ")";
}
}
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 + ")";
}
}
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 john = new Person("John", "1990");
System.out.println(john.toString());
Person jamie = new Student("Jamie", "2005", 12, 3.5, "football");
System.out.println(jamie.toString());
Person jessica = new Teacher("Jessica", "1980", "math", 10, "PhD in Applied Math");
System.out.println(jessica.toString());