抽象クラスと非抽象クラスの違いは、前者をインスタンス化することはできず、オーバーライドする必要があることです。基本クラスのインスタンスがそれ自体で意味をなすかどうかを判断するのは、実際にはあなた次第です。
2つのケースをお見せしましょう。1つは抽象クラスが理にかなっている場合と、そうでない場合です。
public abstract class Animal {
public string Name {get;set;}
}
public class Dog : Animal {
public Bark(){}
}
public class Cat : Animal {
public Meaow(){}
}
このシナリオでは、プロパティAnimal
の実装を提供する共通ベースがありName
ます。動物だけである動物は世界に存在しないので、動物を単独でインスタンス化することは意味がありません。それらは枯れた犬や猫、または他の何かです。
これは、非抽象ベースを持つことが理にかなっている場合です。
class Path {
public Path(IList<Point> points) {
this.Points = new ReadOnlyCollection<Point>(points);
}
public ReadOnlyCollection<Point> Points {get;}
}
class RectanglePath : Path{
public SquarePath (Point origin, int height, int width) :
base(new List<Point>{origin, new Point(origin.X + width, point.Y}, ....){
}
}
ここでは、サブクラス化されていないパスが理にかなっています。任意の形状を作成できますが、より具体的な形状にはサブラスを使用する方が便利な場合があります。