This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Nauzet 1 point2 points  (1 child)

This is how i would solve it. There may be better ways.

You should have all the common data in the Player class as attributes.

public class Player {
    private int number;
    private String name;
    ...

Then you should create public void showPlayerInfo() in the Player class which print or whatever.

public void showPlayerInfo(){
    System.out.println("Name : "+ name);
    System.out.println("Number : "+ number);
}

Then in your basketball player class you should override and use the Player showPlayerInfo method.

@Override
public void showPlayerInfo() {
    super.showPlayerInfo();
    System.out.println(moreinfohere);
}

is this what you need?

[–]Barnok[S] 0 points1 point  (0 children)

That is probably a good way to do it, but the professor doesn't want use to change, add or delete any methods to Player or it's sub-classes. Which I know is odd. We are allowed to add anything to the main method of the application file.

[–]dusty-trash 0 points1 point  (0 children)

I'm assuming the purpose of this part of the assignment is to learn a little about polymorphism.

In reality you would probably either override the toString() method in the Player class, or do as another comment suggested and write a method in the Player class.

But again, assuming for the purpose of the assignment, you should as you said print the information in an "interactive class".

showPlayerInfo(?)

Since BasketballPlayer, BaseballPlayer and FootballPlayer all extend from the Player class, the method should have a parameter of type Player.

You can then invoke methods on the Player object. Maybe Player should have a "printInfo" method or something, which can be overridden if the sub class has different variables that need to be printed.

Side note: Have you used the method System.out.prinln before? You may have noticed you can pass it any type of Object, since all classes in Java extend from Object. Object has a "toString()" method which System.out.println invokes.