Creating an abstract class that implements multiple interfaces in c#

[亡魂溺海] 提交于 2019-12-05 06:07:55

Add abstract methods:

    public interface IPartImportsSatisfiedNotification
    {
        void SomeMethod();
    }

    public abstract class MyClass : IDisposable, IPartImportsSatisfiedNotification
    {
        private string name;
        public MyClass(string name)
        {
            this.name = name;
        }

        public abstract void SomeMethod();

        public abstract void Dispose();
    }

    public class SubClass : MyClass
    {
        public SubClass(string someString) : base(someString)
        {

        }

        public override void SomeMethod()
        {
            throw new NotImplementedException();
        }

        public override void Dispose()
        {
            throw new NotImplementedException();
        }
    }

This is the right way to do it.

    public abstract class MyClass : IDisposable, IPartImportsSatisfiedNotification
    {
        private string name;
        public MyClass(string name)
        {
            this.name = name;
        }

        public abstract void Dispose();
    }

I dont know the definition of your IPartImportsSatisfiedNotification interface so i my sample can only provide the methods defined in IDisposable... Do it for IPartImportsSatisfiedNotification the same way.

You will need to add abstract methods that "implement" those interfaces.

So for instance:

 public abstract void Dispose(); // implements IDisposable

You can just declare the methods and properties the interfaces expect as abstract in your abstract class. This forces the subclasses to still do the implementation but doesn't violate C#'s rules of interfaces.

abstract class in basics its a normal class so he also has to implements these methods.

if you want further implementations , put the virtual methods ( or abstract) in the abstract class itself

As noted by others, you would need to mark the methods as abstract in your base class, which will force derived classes to implement. You can run this as a C# program in LinqPad

void Main()
{   
DerivedClass dc = new DerivedClass("hello, world");
Console.Out.WriteLine(dc);  
string result = dc.Notify("greetings");
Console.Out.WriteLine(result);
}



public interface IPartImportsSatisfiedNotification
{
string Notify(string msg);
}



public abstract class MyClass  : IPartImportsSatisfiedNotification
{
   protected string name; 
   public MyClass(string name)
   {
   this.name = name; 
   }

   abstract public string Notify(string msg);

 }

 public class DerivedClass : MyClass
 {
public DerivedClass(string name) :base(name)
{

}

public override string Notify(string msg)
{
    return string.Format("Msg {0} from {1}", msg, this.name);
}

public override string ToString() 
{
    return this.name;
}
 }

you need to add abstract method in your abstract class.

 public abstract class MyClass : IDisposable, IPartImportsSatisfiedNotification
     {
       private string name; 
       public MyClass(string name)
       {
           this.name = name; 
       }
        public abstract void dispose();
        public abstract void OnImportsSatisfied();

     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!