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?

Dani AI

Generated

Yes — an abstract class can implement an interface, and that interface type can be used to talk to any concrete subclass. There are three common patterns to pass the interface contract down the hierarchy; choose the one that matches whether the base should force, provide, or hide an implementation.

Example (forces derived classes to implement):

interface IAction { void Act(); }

abstract class Worker : IAction
{
    public abstract void Act();   // derived classes must override
}

class Cleaner : Worker
{
    public override void Act() { /* concrete behaviour */ }
}

IAction a = new Cleaner();
a.Act(); // works for any Worker-derived type

Notes on the other patterns (builds on and ):

  • Provide a default: implement the interface method in the abstract class as a public virtual member so subclasses may override and optionally call base. This is best when most subclasses share common logic.
  • Hide/replace (new): avoids compiler errors but is confusing for callers — avoid unless you intentionally want different compile-time behaviour and understand the hiding semantics.
  • Explicit interface implementation: useful to keep the method off the public API (callers must cast to the interface). Explicit implementations cannot be declared virtual/abstract, so derived classes cannot override them — they would need to re-implement the interface.

Troubleshooting checklist: if an override doesn't run, check method signatures exactly match the interface, ensure the base method is virtual/abstract (not hidden with new), and watch for compiler warnings about “hiding” vs “overriding.” Use abstract to require derived implementation, virtual for optional overrides, and avoid new except for rare cases.

Recommended Answers

All 2 Replies

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.