how to make a super class c# code example
Example 1: c# superclass constructor
// Create Constructor in superclass
public Vehicle( double speed )
{
Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
}
//Original Constructor
class Sedan
{
public Sedan(double speed)
{
Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
Wheels = 4;
}
// Remove 'Speed' and 'LicensePlate' from Sedan Constructor
// Add 'class Sedan : Vehicle '
// Add 'public Sedan(double speed) : base(speed)'
//New Constructor
class Sedan : Vehicle
{
public Sedan(double speed) : base(speed)
{
Wheels = 4;
}
}
Example 2: c# implement a superclass in subclass
class Sedan : Vehicle
{
}
// class Sedan will now inherit Vehicles' members
class Sedan : Vehicle, IAutomobile
{
}
// Sedan will now inherit Vehicles' members and promise to
// implement the functions of interface IAutomibile