If I have Person class which will handle all common properties
Person.cs
public class Person
{
public FirstAndLastName {get; set;}
public Health Health {get; set;}
public Personality Personality {get; set;}
}
now I will need Player objects and later on different types of players (soccerPlayer, bballplayer, etc.) Since player is a person can I use this approach
Player.cs
public class Player:Person
{
public Sport Sport {get; set;}
}
Now I want to implement some sport based player unique properties like: in basketball alley up , or volley shoot in football (soccer). You’ve got the pic.
Basketball.cs
public class Basketball:Player
{
public int AlleyUpLevel {get; set;}
}
Question: is this proper way of creating base and derived classes, should my basketball player have access to all properties defined in Person class ?
Thanks
This is the correct way to create base and derived classes. Because your class
Basketballinherits fromPlayerwhich inherits fromPerson,Basketballwill indeed have access to everything defined inPersonthat is markedpublicorprotected. Anything markedprivatewill not be accessible to inheriting classes. Also keep in mind that in the case of many constructs such as fields and methods, the lack of an access modifier will default to aprivateaccess level.