Notes

  • Capability of a class to derive properties and characteristics from another class
  • keyword super
  • extend a subclass and make it a super class
  • @Override in indicate that you are overriding a method
    • making code readable
  • Fruit fruit1 = new Apple
    • Reference type is declared with the superclass
    • Object is declared with the subclass
  • Polymorphism - multiple classes that are related by inheritance
  • superclass contains common methods
    • .equals()
    • .toString()

Syntax

  • public class A
  • public class B extends A

Homework

Part 1:

  • [X] Create a world cup superclass with properties of your choice and subclasses for five teams which inherits those properties
  • [X] Write a constructor for one of those subclasses
public class WorldCup
{
    protected String _name;

    public WorldCup(String name)
    {
        _name = name;
    }

    public String getName()
    {
        return _name;
    }

    public String toString()
    {
        String desctription = "Team name: " + _name;

        return desctription;
    }
    
}
public class NextGame extends WorldCup
{
    private String _time;
    
    public NextGame(String name, String time)
    {
        super(name);

        _time = time;
    }

    @Override
    public String toString()
    {
        String desctription = "Team name: " + _name
        + "\nNext Game: " + _time;

        return desctription;
    }
}
public class Main
{
    public static void main(String[] Args)
    {
        System.out.printf("Superclass:\n");
        WorldCup worldCup = new WorldCup("Croatia");
        System.out.printf("%s\n", worldCup.toString());

        System.out.printf("\nSubclass:");
        NextGame nextGame = new NextGame("Croatia", "12/13/22");
        System.out.printf("\n%s\n", nextGame.toString());
    }
}
Main.main(null);
Superclass:
Team name: Croatia

Subclass:
Team name: Croatia
Next Game: 12/13/22

Part 2:

  • [X] Add a getAge method in the Person super class
  • [X] Create a new subclass Student with additional members of your choice to personalize the Student class
  • [X] Create a new subclass Teacher with additional members of your choice
  • [X] Override the toString method using the @Override to print a Student and teacher object with new members
  • [X] Print the student and teacher.
public class Person
{
    private String _name;
    private int _age;
 
    public Person (String name)
    {
      _name = name;
    }
 
    public String getName()
    {
       return _name;
    }
 }

public class Student extends Person
 {
    private double _gpa;
 
    public Student (String name, double gpa)
    {
      super(name);
      _gpa = gpa;
    }
 
    public double getGPA()
    {
      return _gpa;
    }

    @Override
    public String toString()
    {
      return "code code code";
    }
 }

public class Teacher extends Person
 {
   private String _className;

   public Teacher (String name, String className)
   {
      super(name);
      _className = className;
   }

    public String getClassName()
    {
      return _className;
    }

    @Override
    public String toString()
    {
      return "code code code";
    }
 }

public class Test
 {
   public static void main(String[] Args)
    {
      System.out.printf("Person:\n");
      Person person = new Person("Natalie");
      System.out.printf("%s\n\n", person.getName());

      System.out.printf("Student:\n");
      Student student = new Student("Natalie", 3.9);
      System.out.printf("%.2f\n", student.getGPA());

      System.out.printf("%s\n\n", student);

      System.out.printf("Teacher:\n");
      Teacher teacher = new Teacher("Mr. M", "Computer Science");
      System.out.printf("%s\n", teacher.getName());
      System.out.printf("%s\n", teacher.getClassName());

      System.out.printf("%s\n\n", teacher);
    }
 }

 Test.main(null);
Person:
Natalie

Student:
3.90
code code code

Teacher:
Mr. M
Computer Science
code code code