Hi I am a little confused about interfaces.

Can an abstract class inherit an interface, so that interface can be used in conjunction of a class derived from the adstract?

eg A is an interface, B is an abstarcat class, C & D inherit from B but have different proerties.

Can A still be utilised to interface with C and D, if so how will the one method in the interface be passed down through the adstract class and will the derived classes have to overide the method?

An interface is a collection of un-implemented methods. An interface contains Method, Events, Properties, and Indexers declaration.

for example,

public interface ISample{
     void show();  // Method
     int this[int n]{get;set;} // Indexer
     int No { get;set;} // Property
     event EventHandler changed; // Event
 }

An interface defines a contract. A class that implements an interface must adhare to its contract.

Consider the following example:

I have two interfaces namely IShape and IDimension.

IShape interface

public interface IShape{
   void Draw();
}

IDimension interface

public interface IDimension {
    int Left{get;set;}
    int Top{get;set;}
    int Width{get;set;}
    int Height{get;set;}
}

Now, a class "Circle" implements two interfaces - i.e Class "Circle" must have to implements (defines) Draw(), Left, Top, Width, and Height methods.

public class Circle : IShape, IDimension {
       public void Draw() {
             ....
       }
      public int Left { get { ..} set { ..} }
      public int Top { get { ..} set { ..} }
      public int Height { get { ..} set { ..} }
      public int Width { get { ..} set { ..} }
  }

Note: If class "Circle" fails to implement all the methods from interfaces then it must be marked with an abstract modifier - An Abstract class.

You can go about this two ways. You can mark the interface implementation in the abstract class as virtual and override it in descending classes:

namespace daniweb
{
  interface IDoWork
  {
    void DoWork();
  }

  public abstract class ClassB : IDoWork
  {
    #region A Members
    public virtual void DoWork()
    {
      Console.WriteLine("ClassB.DoWork()");
    }
    #endregion
  }

  public class ClassC : ClassB
  {
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public override void DoWork()
    {
      Console.WriteLine("ClassC.DoWork()");
      base.DoWork();
    }
  }

  public class ClassD : ClassB
  {
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }
    public override void DoWork()
    {
      Console.WriteLine("ClassD.DoWork()");
      base.DoWork();
    }
  }
}

Or you can implement the Interface in the abstract class and the descending class. The compiler will give you a warning since you are hiding the interface for the base class but since this is intended you can add the "new" keyword to the interface implementation in the descending classes:

namespace daniweb
{
  interface IDoWork
  {
    void DoWork();
  }

  public abstract class ClassB : IDoWork
  {
    #region A Members
    public void DoWork()
    {
      Console.WriteLine("ClassB.DoWork()");
    }
    #endregion
  }

  public class ClassC : ClassB, IDoWork
  {
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    #region IDoWork Members
    public new void DoWork()
    {
      Console.Write("ClassC.DoWork()");
      base.DoWork();
    }
    #endregion
  }

  public class ClassD : ClassB, IDoWork
  {
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }

    #region IDoWork Members
    public new void DoWork()
    {
      Console.Write("ClassD.DoWork()");
      base.DoWork();
    }
    #endregion
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.