Reputation: 4160
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;
}
}
Upvotes: 6
Views: 7129
Reputation:
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();
}
Upvotes: 0
Reputation: 6738
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;
}
}
Upvotes: 0
Reputation: 8631
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();
}
}
Upvotes: 9
Reputation: 148514
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
Upvotes: 0
Reputation: 60438
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.
Upvotes: 7
Reputation: 39255
You will need to add abstract methods that "implement" those interfaces.
So for instance:
public abstract void Dispose(); // implements IDisposable
Upvotes: 1
Reputation: 6955
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.
Upvotes: 0