illustrates deriving an interface from multiple interfaces


   

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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example8_6.cs illustrates deriving an
  interface from multiple interfaces
*/

using System;



public class Example8_6
{

  public static void Main()
  {

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

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

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

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

  }

}


// define the IDrivable interface
public interface IDrivable
{

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

  // property declaration
  bool Started
  {
    get;
  }

}


// define the ISteerable interface
public interface ISteerable
{

  // method declarations
  void TurnLeft();
  void TurnRight();

}


// define the IMovable interface (derived from IDrivable and ISteerable)
public interface IMovable : IDrivable, ISteerable
{

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

}


// Car class implements the IMovable interface
public 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 TurnLeft() method of the ISteerable interface
  public void TurnLeft()
  {
    Console.WriteLine("car turning left");
  }
  
  // implement the TurnRight() method of the ISteerable interface
  public void TurnRight()
  {
    Console.WriteLine("car turning right");
  }

  // 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");
  }

}

           
          


illustrates deriving an interface from one interface


   

/*
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();

  }

}

           
          


illustrates casting an object to an interface


   

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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example8_4.cs illustrates casting an object
  to an interface
*/

using System;


// define the IDrivable interface
interface IDrivable
{

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

  // property declaration
  bool Started
  {
    get;
  }

}


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

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

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

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

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


public class Example8_4
{

  public static void Main()
  {

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

    // use the is operator to check that myCar supports the
    // IDrivable interface
    if (myCar is IDrivable)
    {
      Console.WriteLine("myCar supports IDrivable");
    }

    // cast the Car object to IDrivable
    IDrivable myDrivable = (IDrivable) myCar;

    // call myDrivable.Start()
    Console.WriteLine("Calling myDrivable.Start()");
    myDrivable.Start();
    Console.WriteLine("myDrivable.Started = " +
      myDrivable.Started);

    // call myDrivable.Stop()
    Console.WriteLine("Calling myDrivable.Stop()");
    myDrivable.Stop();
    Console.WriteLine("myDrivable.Started = " +
      myDrivable.Started);

    // cast the Car object to IDrivable using the as operator
    IDrivable myDrivable2 = myCar as IDrivable;
    if (myDrivable2 != null)
    {
      Console.WriteLine("Calling myDrivable2.Start()");
      myDrivable2.Start();
      Console.WriteLine("Calling myDrivable2.Stop()");
      myDrivable2.Stop();
      Console.WriteLine("myDrivable2.Started = " +
        myDrivable2.Started);
    }

  }

}


           
          


illustrates inheriting from a class and implementing multiple interfaces


   

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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example8_3.cs illustrates inheriting from a class and
  implementing multiple interfaces
*/

using System;


interface IDrivable
{

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

  // property declaration
  bool Started
  {
    get;
  }

}


interface ISteerable
{

  // method declarations
  void TurnLeft();
  void TurnRight();

}


class MotorVehicle
{

  // declare the field
  private string model;

  // define a constructor
  public MotorVehicle(string model)
  {
    this.model = model;
  }

  // declare a property
  public string Model
  {
    get
    {
      return model;
    }
    set
    {
      model = value;
    }
  }

}


// Car class inherits from the MotorVehicle class and
// implements the IDrivable and ISteerable interfaces
class Car : MotorVehicle, IDrivable, ISteerable
{

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

  // define a constructor
  public Car(string model) :
  base(model)   // calls the base class constructor
  {
    // do nothing
  }

  // 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 TurnLeft() method of the ISteerable interface
  public void TurnLeft()
  {
    Console.WriteLine("car turning left");
  }
  
  // implement the TurnRight() method of the ISteerable interface
  public void TurnRight()
  {
    Console.WriteLine("car turning right");
  }

}


public class Example8_3
{

  public static void Main()
  {

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

    Console.WriteLine("myCar.Model = " + myCar.Model);

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

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

  }

}


           
          


illustrates implementing multiple interfaces


   

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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example8_2.cs illustrates implementing multiple interfaces
*/

using System;


// define the IDrivable interface
public interface IDrivable
{

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

  // property declaration
  bool Started
  {
    get;
  }

}


// define the ISteerable interface
public interface ISteerable
{

  // method declarations
  void TurnLeft();
  void TurnRight();

}


// Car class implements the IMovable interface
class Car : IDrivable, ISteerable
{

  // 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 TurnLeft() method of the ISteerable interface
  public void TurnLeft()
  {
    Console.WriteLine("car turning left");
  }
  
  // implement the TurnRight() method of the ISteerable interface
  public void TurnRight()
  {
    Console.WriteLine("car turning right");
  }

}


public class Example8_2
{

  public static void Main()
  {

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

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

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

  }

}


           
          


illustrates interfaces


   

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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example8_1.cs illustrates interfaces
*/

using System;


// define the IDrivable interface
public interface IDrivable
{

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

  // property declaration
  bool Started
  {
    get;
  }

}


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

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

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

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

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


public class Example8_1
{

  public static void Main()
  {

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

    // call myCar.Start()
    myCar.Start();
    Console.WriteLine("myCar.Started = " + myCar.Started);

    // call myCar.Stop()
    myCar.Stop();
    Console.WriteLine("myCar.Started = " + myCar.Started);

  }

}


           
          


Using interface 3

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System;

// An encryption interface.
public interface ICipher {
string encode(string str);
string decode(string str);
}

/* A simple implementation of ICipher that codes
a message by shifting each character 1 position
higher. Thus, A becomes B, and so on. */
class SimpleCipher : ICipher {

// Return an encoded string given plaintext.
public string encode(string str) {
string ciphertext = “”;

for(int i=0; i < str.Length; i++) ciphertext = ciphertext + (char) (str[i] + 1); return ciphertext; } // Return an decoded string given ciphertext. public string decode(string str) { string plaintext = ""; for(int i=0; i < str.Length; i++) plaintext = plaintext + (char) (str[i] - 1); return plaintext; } } /* This implementation of ICipher uses bit manipulations and key. */ class BitCipher : ICipher { ushort key; // Specify a key when constructing BitCiphers. public BitCipher(ushort k) { key = k; } // Return an encoded string given plaintext. public string encode(string str) { string ciphertext = ""; for(int i=0; i < str.Length; i++) ciphertext = ciphertext + (char) (str[i] ^ key); return ciphertext; } // Return an decoded string given ciphertext. public string decode(string str) { string plaintext = ""; for(int i=0; i < str.Length; i++) plaintext = plaintext + (char) (str[i] ^ key); return plaintext; } } // Use ICipher. // A class for storing unlisted telephone numbers. class UnlistedPhone { string pri_name; // supports name property string pri_number; // supports number property ICipher crypt; // reference to encryption object public UnlistedPhone(string name, string number, ICipher c) { crypt = c; // store encryption object pri_name = crypt.encode(name); pri_number = crypt.encode(number); } public string Name { get { return crypt.decode(pri_name); } set { pri_name = crypt.encode(value); } } public string Number { get { return crypt.decode(pri_number); } set { pri_number = crypt.encode(value); } } } // Demonstrate UnlistedPhone public class UnlistedDemo { public static void Main() { UnlistedPhone phone1 = new UnlistedPhone("Tom", "555-3456", new BitCipher(27)); UnlistedPhone phone2 = new UnlistedPhone("Mary", "555-8891", new BitCipher(9)); Console.WriteLine("Unlisted number for " + phone1.Name + " is " + phone1.Number); Console.WriteLine("Unlisted number for " + phone2.Name + " is " + phone2.Number); } } [/csharp]