illustrates deriving an interface from one interface

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example8_5.cs illustrates deriving an
  interface from one interface
*/

using System;


// define the IDrivable interface
interface IDrivable
{

  // method declarations
  void Start();
  void Stop();

  // property declaration
  bool Started
  {
    get;
  }

}


// define the IMovable interface (derived from IDrivable)
interface IMovable : IDrivable
{

  // method declarations
  void Accelerate();
  void Brake();

}


// Car class implements the IMovable interface
class Car : IMovable
{

  // declare the underlying field used by the
  // Started property of the IDrivable interface
  private bool started = false;

  // implement the Start() method of the IDrivable interface
  public void Start()
  {
    Console.WriteLine("car started");
    started = true;
  }

  // implement the Stop() methodof the IDrivable interface
  public void Stop()
  {
    Console.WriteLine("car stopped");
    started = false;
  }

  // implement the Started property of the IDrivable interface
  public bool Started
  {
    get
    {
      return started;
    }
  }

  // implement the Accelerate() method of the IMovable interface
  public void Accelerate()
  {
    Console.WriteLine("car accelerating");
  }
  
  // implement the Brake() method of the IMovable interface
  public void Brake()
  {
    Console.WriteLine("car braking");
  }

}


public class Example8_5
{

  public static void Main()
  {

    // create a Car object
    Car myCar = new Car();

    // call myCar.Start()
    Console.WriteLine("Calling myCar.Start()");
    myCar.Start();

    // call myCar.Accelerate()
    Console.WriteLine("Calling myCar.Accelerate()");
    myCar.Accelerate();

  }

}