• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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: 9348
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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.
 
Saloon Keeper
Posts: 14010
315
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic