6

I'd like to create an abstract class in c#, that "inherits" from different interfaces, but leaves the concrete implementation to the subclass. The compiler however complains, that the class doesnt implement the methods specified in the interfaces. I'm used to Java where this always worked, so I'm not sure how it is supposed to work in c#. Anyway, this is my code:

 public abstract class MyClass : IDisposable, IPartImportsSatisfiedNotification
 {
   private string name; 
   public MyClass(string name)
   {
       this.name = name; 
   }
 }
1
  • So add in the methods needed but tag them abstract Commented Oct 22, 2011 at 20:42

7 Answers 7

9

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();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

1

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

So for instance:

 public abstract void Dispose(); // implements IDisposable

Comments

0

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.

Comments

0

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

Comments

0

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;
}
 }

Comments

0

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

     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.