Search code examples
objectreferencesubclasssuperclass

Get Stored Data From Superclass to Subclass Object


I have a super class "Player" and 3 sub classes "BaseballPlayer", "FootballPlayer", and "BasketballPlayer"... To make things simple, I will only share "Player" and "BaseballPlayer".

I am trying to store data into an object of these classes, but I can not seem to get it and print it back out.

Player.java

public class Player {

private int id;
private String playerName;
private String teamName;
private String position;
private double salary;
private double commisionRate;

/* Default Constructor */
public Player() {
}

/* Constructor */
public Player(int id, String playerName, String teamName, String position, double salary, double commisionRate) {
    this.id = id;
    this.playerName = playerName;
    this.teamName = teamName;
    this.position = position;
    this.salary = salary;
    this.commisionRate = commisionRate;
}

/* Getters */ 
public int getID() {
    return id;
}

public String getPlayerName() {
    return playerName;
}

public String getTeamName() {
    return teamName;
}

public String getPosition() {
    return position;
}

public double getSalary() {
    return salary;
}

public double getCommisionRate() {
    return commisionRate;
}

/* Setters */
public void setID(int id) {
    this.id = id;
}

public void setPlayerName(String playerName) {
    this.playerName = playerName;
}

public void setTeamName(String teamName) {
    this.teamName = teamName;
}

public void setPosition(String position) {
    this.position = position;
}

public void setSalary(double salary) {
    this.salary = salary;
}

public void setCommision(double commisionRate) {
    this.commisionRate = commisionRate;
}

/* Effectors */
public double calcCommision()
{
    return salary * commisionRate;
}

}

BaseballPlayer.java

public class BaseballPlayer extends Player {

public static final double Threshold = 0.25;

private int numOfHits;
private int numAtBat;

/* Default Constructor */
public BaseballPlayer() {
}

/* Constructor */
public BaseballPlayer(int id, String playerName, String teamName, String position, double salary, double commisionRate, int numOfHits, int numAtBat) {
    super(id, playerName, teamName, position, salary, commisionRate);
    this.numOfHits = numOfHits;
    this.numAtBat = numAtBat;
}

/* Getters */
public int getNumOfHits() {
    return numOfHits;
}

public int getNumAtBat() {
    return numAtBat;
}

/* Setters */
public void setNumOfHits(int numOfHits) {
    this.numOfHits = numOfHits;
}

public void setNumAtBat(int numAtBat) {
    this.numAtBat = numAtBat;
}

/* Effectors */
public double calcStats()
{
    return numOfHits / numAtBat;
}

public boolean detStatus()
{
    if(calcStats() > Threshold) {
        return true;
    } else {
        return false;
    }
}

}

Suppose with these two classes I create an object...

Player BaseballPlayer1 = new BaseballPlayer(4, "Jeff Jefferson", "Kentucky Kangaroos", "3rd Base", 340000.00, 0.02, 40, 5);

However, whenever I call to this object for the data, it prints nothing...

System.out.printf("Player ID: ", BaseballPlayer1.getID());

What am I missing? I've looked all over SO and Google, and my IDE shows no errors, but getID() returns no value...


Solution

  • This should work:

    System.out.printf("Player ID: %d", baseballPlayer.getID());
    

    Have a look at the this for example usage of System.out.printf().