Reputation: 5
For fun, I am wanting to make a basketball simulation game in Java. I am wanting to have one simple class "team" and I want to have a subclass of class team named "player". I understand how to declare classes and subclasses but my question is how do I declare a player to a certain team?
For example:
Team team1 = new Team();
Team team2 = new Team();
Player nameHere = new Player();
How do I set it to where Player nameHere is on team1, or team2, etc...
Upvotes: 1
Views: 285
Reputation: 30310
Others have offered great explanations, but let me add something.
A lot of inexperienced developers use subclassing to save code. They are so enamored with the inheritance they learned in school that they are anxious to factor everything out to some common base class and subclass it like crazy.
That is the wrong approach.
A class should subclass another class not if they share code but if they share the same behavior. So ask yourself, does a player have the same behavior as a team?
Probably not.
A team, for example, has a schedule, a list of opponents, an owner, a city, an attendance figure, a stadium, and so on. Players do not. So Player
should not subclass Team
. A Team
, however, makes sense to be composed of a List<Player>
as others have mentioned.
But let's make this more complicated and interesting now.
Let's consider a soccer team. A player on the team can have goals, assists, and fouls. The team could also have all those things, where the team's value for those things is the sum of those values for all the players. But rather than use inheritance, you would be better served using an interface called something like Measurable
(lame name, I know) and have both Team
and Player
implement that interface. Let's say Measurable
has a getGoals()
method. Then if the client says
int goals = measurable.getGoals();
the client doesn't know or care if he is getting the number of goals for a team or a player--just that the object knows how many goals it has.
This in fact is an example of the Composite Pattern.
So the bottom line is that Player
should not sublcass Team
, but Team
should be composed of Player
because they have different behaviors. But if they do have common attributes like name, goals, etc., then use an interface to represent that as a common way to describe entirely different kinds of objects.
Upvotes: 1
Reputation: 48404
If you think of your Team
as a container for Player
s, your Team
class will have a property that contains the Player
s, and accessor methods to add, remove, clear, etc. the Team
's players.
Of course, your Team
class will have its own properties and methods, possibly tapping data from whatever Player
s you have in the team (e.g. the number of players, etc.).
In this instance there is no topic related to inheritance.
Your classes just have a container vs "containee" relationship (Artem's answer uses the proper terminology).
For instance:
class Team {
// the private property containing the players
// I use a Set here because each player is unique
private Set<Player> players;
public Team() {
// initializing the set as HashSet
players = new HashSet<Player>();
}
public void addPlayers(Player... players) {
// adding all players
for (Player p: players) {
this.players.add(p);
}
}
public Collection<Player> getPlayers() {
// returning a copy of the team's players
return new HashSet<Player>(players);
}
}
class Player {
// TODO player's properties such as name, age, feats, etc.
}
Then you'd add Player
s to your Team
as such:
Team t = new Team();
// adding single player
t.addPlayers(new Player());
// adding an array of players
t.addPlayers(new Player[]{new Player(), new Player()});
Upvotes: 0
Reputation: 5818
There are two important software design principles you need to understand:
- Inheritance (IS A relationship)
- Composition (HAS A relationship)
When you deal with the first concept, you use classes and subclasses. But in this case, your subclass is essentially the same as your parent class: it is like a car and Ferrari. Ferrari IS A car (this kind of relationship is represented by inheritance in OOP). Nevertheless, a car HAS A wind screen. Wind screen is not a car, it is its part (this relationship is represented by the inclusion of one class as a field member of another class - composition). Now, that you know about these relationships, you need to ask yourself a question: IS Player A team, or HAS team A Player?
In this case, it seems obvious. You need to place a List<Player>
inside your Team
class.
Upvotes: 2
Reputation: 29
You'll need something similar to this.
class Team {
List<Player> players;
Team() {
players = new ArrayList<Player>();
}
public void addPlayer(Player p) {
players.add(p);
}
}
And this is how you create and add Player
to Team
:
Player player1 = new Player();
Player player2 = new Player();
Team team1 = new Team();
team1.addPlayer(player1);
team1.addPlayer(player2);
Thing to know is inheritance (is a)
vs composition(has a)
relationships.
Hope this helps.
Upvotes: 0
Reputation: 26094
Do like this
class Player {
//player properties and setter , getter
}
class Team{
List<Player> players = new ArrayList<Player>();
// setter and getter
}
Adding Player to the Team
Team t1= new Team();
Player p1 = new Player();
Player p2 = new Player();
Player p3 = new Player();
t1.getPlayers().add(p1);
t1.getPlayers().add(p2);
t1.getPlayers().add(p3);
Upvotes: 0
Reputation: 833
I don't think you fully understand what Inherritance means. A subclass of a superclass should be a special case of the superclass.
Look at http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html for more examples.
What you want is a class team with an attrute
List<Player> players;
I suggest you read up on inherritance if you want to write any object-oriented code.
Upvotes: 1