Rimantas Radžiūnas
Rimantas Radžiūnas

Reputation: 257

Using subclass objects in superclass object

I'm kinda new to Java, so I have a, rather dumb, question: Is it possible to have and use subclass'es objects in superclass which too have a constructor?

Here's an example what I want to do: I have Superclass Team and subclass Player. I want to create Player object, which contains variables like name, surname, height, etc. . And I want to access this Player object in Team class, which have it's name, positions and other variables, to assign the Player object to his position in the Team. Lets say, Player: John Doe is in Team named Red and he is a Goalkeeper.

And all together, is it possible to access all of this in main function? For example I want to print out all Team Red's players names.

Sorry, it's kinda complicated to explain what I want to do, but isn't this have to do something with extends?

Upvotes: 0

Views: 228

Answers (5)

Jomy George
Jomy George

Reputation: 323

When we think about team and player, its relation is team has a player. Use the same relation when you design the classes. Position is a property which is relevant in the scope of a Player. So introduce that property in correct level. Better to keep Team class with its own properties.

Upvotes: 1

techPackets
techPackets

Reputation: 4504

You have to create 3 classes Team, Player & a Client class to input the data. I am writing the skeleton code for you. It will give you an idea as to how to write your code

public class Team {

    ArrayList<Player> player;

    public ArrayList<Player> getPlayer() {
        return player;
    }

    public void setPlayer(ArrayList<Player> player) {
        this.player = player;
    }
}

Player

class Player{

    String name;
    Integer score;
    Integer Rank;

 //getter setters 
}

Client class

public class Client {

    public static void main(String[] args) {

        Player pOne = new Player();
        pOne.setRank(2);
        pOne.setName("ABD");

        Player pTwo=new Player();
        pTwo.setRank(2);
        pTwo.setName("SPC");
        pTwo.setTotal(60);

        ArrayList<Player> playerList = new ArrayList<Player>();
        playerList.add(pOne);
        playerList.add(pTwo);

        Team team=new Team();
        one.setPlayer(playerListOne);

//get the player using Team class's getPlayer method & print
}

You need not use extends but rather focus on the OO design.

Upvotes: 1

kaykay
kaykay

Reputation: 556

What you are looking for is a composition and not inheritance (i.e. A Team "has a" Player. "A Player is a Team" does not make much sense)

class Player {
    String name;
    String surname;
    String height;
}

class Team {
    String name;
    List<Player> players;
}

Upvotes: 1

Kevin Workman
Kevin Workman

Reputation: 42176

The short answer is that yes, this is possible.

The longer answer is that you're misusing the terms superclass and subclass. The relationship you describe is not an is-a relationship of inheritance, but a has-a relationship of composition.

In other words, your Player class does not extend your Team class. Your Team class would contain instances of Player. And yes, in that case your Team class could access the methods and variables of those instances, just like you can any other Java class. It might look something like this:

class Team{
   List<Player> players = new ArrayList<Player>();

   public void addPlayer(Player p, String position){
      p.position = position;
      players.add(p);
   }
}

And then from a main() method, you might do something like this:

public class Main{
   public static void main(String... args){
      Team team = new Team("Fighting Cats");
      Player p = new Player("Johnny Appleseed");
      team.addPlayer(p, "Apple Picker");
   }
}

Upvotes: 4

SamTebbs33
SamTebbs33

Reputation: 5647

Yes, see the below code.

public class Team{
  public Player player;
}

class Player extends Team{
  public Team team;
}

Although, I doubt your logic and design when you say that Player extends Team, which essentially says that a Player is a Team. This would mean that each Player object would have a list of players and positions (as subtypes inherit non-private states and behaviours from supertypes), which doesn't make much sense semantically.

I suggest that you instead approach it like the following.

public class Team{
    public LinkedList<Player> players;
}

class Player{
    public Position position; // Use some way of defining the player's position.
}

This means that Team would have a Player, rather Player being a Team

Upvotes: 1

Related Questions