Creating an abstract class that implements several interfaces in C #

I would like to create an abstract class in C # that "inherits" from different interfaces, but leaves a concrete implementation in a subclass. However, the compiler complains that the class does not implement the methods specified in the interfaces. I'm used to Java, where it always worked, so I'm not sure how it should work in C #. Anyway, this is my code:

public abstract class MyClass : IDisposable, IPartImportsSatisfiedNotification { private string name; public MyClass(string name) { this.name = name; } } 
+6
source share
7 answers

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(); } } 
+9
source

This is the right way to do this.

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

I do not know the definition of your IPartImportsSatisfiedNotification interface, so my example can only provide the methods defined in IDisposable ... Do this for IPartImportsSatisfiedNotification in the same way.

+7
source

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

So for example:

  public abstract void Dispose(); // implements IDisposable 
+1
source

You can simply declare the methods and properties that interfaces expect as abstract in your abstract class. This forces the subclasses to continue execution, but does not violate C # interface rules.

0
source

An abstract class is basically its ordinary class, so it must also implement these methods.

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

0
source

As others have noted, you will need to mark the methods as abstract in your base class that 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; } } 
0
source

you need to add an abstract method to 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(); } 
0
source

Source: https://habr.com/ru/post/899879/


All Articles