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

Homework

  • Write a superclass/subclass for the world cup using different teams
  • Complete the person/student class/subclass homework
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();
Team score: 0
Team wins: 1
Team all goals: 27
Best player: Unknown
Team score: 0
Team wins: 2
Team all goals: 55
Best player: Cristiano Ronaldo
Team score: 0
Team wins: 2
Team all goals: 129
Best player: Kylian Mbappe
Team score: 0
Team wins: 1
Team all goals: 18
Best player: Youssef En-Nesyri
Team score: 0
Team wins: 1
Team all goals: 94
Best player: Memphis Depay
Team score: 0
Team wins: 1
Team all goals: 40
Best player: Luka Modric

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());
Person (name: John, birthday: 1990)
Student (name: Jamie, birthday: 2005, extracurricular: football, gpa:3.5, grade: 12)
Teacher (name: Jessica, birthday: 1980, subject: math, tenureYears:10, degree: PhD in Applied Math)