I’m still learning C# and just had a basic question about inheritance.
Lets say I have an abstract class SportsPlayer:
public abstract class SportsPlayer
{
string name; /*1ai*/
int age; /*1aii*/
Sexs gender; /*1aiii*/
//1b
public SportsPlayer(string n, int a, Sexs g)
{
this.name = n;
this.age = a;
this.gender = g;
}
}
And a subclass called SoccerPlayer:
public class SoccerPlayer : SportsPlayer
{
Positions position;
public SoccerPlayer(string n, int a, Sexs g, Positions p)
: base(n, a, g)
{
this.position = p;
}
//Default constructor
public SoccerPlayer()
{
}
Is it possible to create a constructor on the subclass which is passed no arguments or am I right in thinking that in order to create a default constructor on a subclass, the super class must have a default constructor too?
Also, if I were to add a default constructor to the super class, how would I initialize the super class variables in the subclass constructor? In java its super(), in C# it’s??
public SoccerPlayer():base()
{
base.name = "";
}
???
You can create a parameterless constructor on the derived class, but it still needs to pass arguments to the base class constructor:
Or you could add a default constructor to the base class…