954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Interface for abstract class

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?

FallenPaladin
Junior Poster in Training
62 posts since Oct 2007
Reputation Points: 10
Solved Threads: 2
 

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.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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
  }
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You