• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Inheritance with mulltiple class types

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the best approach using inheritance if i wish for a player to be part of a multiple sports teams which also allows for player/coach? I'm using arraylists at the moment.
 
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a "Member" class then a "Player" is-a Member and a "Coach" is-a Member. Then a Member could have a list of Teams that he/she is associated with. Or, conversely, a Team could have a list of Members. Or both, which would require code to keep them in sync.
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's like asking: "What's the best approach using a saw when I need to hammer a nail?"

Why are you settling on a tool before you've analyzed the problem?

In this case, I would say that a sports team has players and a coach, both of which are persons. No inheritance needed, unless coaches and players have different properties. Even then, you can use roles instead of inheritance.

Why do you think you need inheritance?
 
Jason Marvin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's like asking: "What's the best approach using a saw when I need to hammer a nail?"

Why are you settling on a tool before you've analyzed the problem?

In this case, I would say that a sports team has players and a coach, both of which are persons. No inheritance needed, unless coaches and players have different properties. Even then, you can use roles instead of inheritance.

Why do you think you need inheritance?



Hi Stephan, I am learning Java online and the task is to use inheritance to modal a sports club. Are you able to expand on what you said about using roles or provide a link that i can read up on?
 
Jason Marvin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, for the given example, soccer players score goals and basketball players score points?
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This would not be unlike the Singer, Songwriter, SingerSongwriter example in Effective Java, item 20. What Bloch says there would be applicable, and he's basically saying that you should use interfaces for Player and Coach, then if necessary, your "PlayerCoach" could implement both interfaces.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic