Example for Interface :
Code:
public interface IVehicle
{
void Start();
void Drive();
void Park();
void ChangeGear(int gear);
void SwitchOff();
}
public class Vehicle : IVehicle
{
public void Start()
{
Console.WriteLine("The vehicle has been started");
}
public void Drive()
{
Console.WriteLine("The vehicle is being driven");
}
public void Park()
{
Console.WriteLine("The vehicle is being parked");
}
public void ChangeGear(int gear)
{
Console.WriteLine("Gear changed to " + gear.ToString());
}
public void SwitchOff()
{
Console.WriteLine("The vehicle has been switched off");
}
}
Example for Abstract class :
Code:
public abstract class Vehicle
{
public void Start()
{
Console.WriteLine("The vehicle has been started");
}
public abstract void Drive();
public abstract void Park();
public abstract void ChangeGear(int gear);
public void SwitchOff()
{
Console.WriteLine("The vehicle has been switched off");
}
}
public class Car : Vehicle
{
public Car()
{
}
public override void Drive()
{
Console.WriteLine("The car is being driven");
}
public override void Park()
{
Console.WriteLine("The car is being parked");
}
public override void ChangeGear(int gear)
{
Console.WriteLine("The car changed gear changed to " + gear.ToString());
}
}
No comments:
Post a Comment