Allan Didier

Inheritance Intro

Goals

To write classes that can inherit properties from another class.

Skills

  • Inheritance

Program Details

Write a program which can instantiate, modify, and output statistics for a few sports players. You can choose your own statistics or the use ones provided below. Your program will need the following:

  1. A Player Class with
    1. 3 private variables for the player’s name, salary, and years played.
    2. A constructor method that instantiates the variable by passing in values. You don’t need to ask input from the console.
    3. Public setter and getter methods to change and output the 3 variables. You can use the Eclipse Source-Generate Setters and Getters to accomplish this.
    4. A toString() method to output the player information to a string.
  2. Three Child Classes that inherit the Player class, such as a Quarterback, Running Back, and Field Goal Kicker classes.
    1. The child classes should each have
      1. 3 private variables specific to the class. Examples could be:
        1. Quarterback: touch downs thrown, pass percentage, and yards per game.
        2. Running Back: touch downs made, yards per carry, and longest run.
        3. Field Goal Kicker: field goals made, field goal percentage, and longest field goal made.
      2. A constructor method that instantiates the 3 variables above by passing them directly in. It will also need to pass in the three Player variables and run the Player constructor method to instantiate the Player variables. You don’t need a constructor that asks the user for input.
      3. Public setter and getter methods for each of these variables. Setters should report that a change has been made.
      4. A toString method to output the player information to a string. This method should call the parent toString method to output the Player information.
  3. A Main Program that
    1. Creates an Array or ArrayList of players (the parent class).
    2. Instantiates 3 different Players into the list, one from each child class. The player data can be instantiated directly in the code. You do not need to get input from the console.
    3. Modifies each player twice. Use a parent setter and a child setter for each player modification.
    4. Outputs the initial and final player stats to the console.

 

Resources

Edhesive Term 2

Edhesive T2 Lesson 7:  Inheritance

Edhesive T2 Lesson 8:  Method Overriding

AP College Board:  Creating Superclasses and Subclasses

AP College Board:  Writing Constructors for Subclasses

AP College Board:  Overriding Methods