+ 2
When we should use abstract class?
2 Antworten
+ 3
When you want to share the implementation of a class between multiple classes.
So, if you three classes that all had a property titled "name", and a method titled "print"
Such as:
class One{
private string name {get;set}
public void Print() { Console.WriteLine(this.name);}
}
class Two{
private string name {get;set}
public void Print() { Console.WriteLine(this.name);}
}
class Three{
private string name {get;set}
public void Print() { Console.WriteLine(this.name);}
}
As we can see all three classes would share the same piece of implementation which leads to unnecessary duplication. We can abstract that code out into an abstract class and have our three classes derive from that.
However, if you only want to share the definition IE I need three classes with a method "print", but I don't care how thats implemented, and have no other shared implementation you'd use an interface. You can also mark methods as abstract and force the deriving class to provide its own implementation, but I digress.
+ 1
Thanks