0

I'm trying to learn interface and base classes on practical example. Let's say that I want to to abstract Player entity On Player.cs should be all common properties and methods for every sport in which player is assigned. So, there will be TeamSportPlayer, IndividualSportPlayer. Again, FootballPlayer would derive from TeamSportPlayer, TennisPlayer would derive from IndividualSportPlayer and so on. All this players should have access to first class Player and their properties.

Hope I'm not too confusing.

Question is: Is this proper way of abstracting player representation in terms of oop?

How would you do this on this practical example?

dove
  • 20,469
  • 14
  • 82
  • 108
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • Do you want to know what the difference between an interface and (abstract) classes is or do you need some design advice where to use (abstract) classes and interfaces? – Jay Nov 25 '12 at 20:10
  • advices where to use abstract and where to use interfaces. – user1765862 Nov 25 '12 at 20:12
  • 1
    That kind of advice depends on many concrete items in your actual codebase - it is not something we can give here. – Oded Nov 25 '12 at 20:16
  • @Oded Can you be more specific. It will be great if someone can show me which design pattern should I follow, having this (described earlier) situation in mind. I dont need the code – user1765862 Nov 25 '12 at 20:18
  • Well, that's exactly it - can _you_ be more specific. On Stack Overflow, if a question doesn't have code in it, it will more often than not be off-topic. It is really not clear what you are trying to achieve and the question reads as an attempt at a solution rather than a description of a problem. For questions that are not about code but things like design and such [programmers](http://programmers.stackexchange.com/) may be a better fit (read the FAQ before posting though). – Oded Nov 25 '12 at 20:21

1 Answers1

2

Abstract classes are used for defining objects that you are never going to have an instance of. Interfaces on the other hand are used to define behaviour of objects, and interfaces are independent from the inheritance hierarchy.

Using your sports example:

Player.cs can be an abstract class. It has fields that every player has like name, age, address, etc. But you never have a "Player" on the sports field, you have a "Football player" or a "Basketball player". And the classes FootballPlayer.cs and BasketballPlayer.cs inherit from the abstract class Player.cs.

Interface on the other hand defines some common behaviour that the classes share. Usually its used to define how other classes can interact with them. So for instance, if you have classes called TennisPlayer.cs, BasketballPlayer.cs and FootballPlayer.cs you can have an interface called IHasJerseyNumber.cs. Basketball and football players have jersey numbers so they would inherit the IHasJerseyNumber.cs interface. Tennis players don't have a number and they wont inherit the interface. A totally seperate class like Referee.cs can implement the interface as well, providing he too has a jersey number (possible in some sports).

You can read more here:

Interfaces

Abstract classes

Community
  • 1
  • 1
AndrejKolar
  • 284
  • 1
  • 9