"abstract class" versus "normal class" for reusable library
I am developing a reusable library and creating abstract classes so the client can then migrate from them.
QUESTION: Is there some reason why I should be using an abstract class here and not just a regular class?
Note. I've already decided that I don't want to use interfaces as I want to include actual default methods in my library, so the client using it doesn't need to write any code.
EDIT: So, I am catching any benefits that I cannot think of. For example, when updating a library, using an abstract class will reduce the impact on client code - I can't see, which in this case is not?
a source to share
The motivation for an abstract class is to require clients to override the class. Your decision as to whether a class should be abstract or not depends primarily on whether the abstract is missing some fundamental behavior that only the user of the class can provide.
You can usually tell your class to be abstract if you use some kind of boilerplate method where "plugging a hole in this behavior" completes the class logic. If your class is useful without any user-supplied logic, you probably don't need an abstract class.
As an example, frameworks usually cannot make decisions on behalf of their users in terms of validating state, printing, displaying, etc., and will need to refer to specific client implementations.
a source to share
The difference between an abstract class and a non-abstract one is that you cannot instantiate the former and it MUST be overridden. It's really up to you to determine if an instance of the base class actually makes sense.
Let me show you two cases. One of them is an abstract class that makes sense and one where it is missing.
public abstract class Animal {
public string Name {get;set;}
}
public class Dog : Animal {
public Bark(){}
}
public class Cat : Animal {
public Meaow(){}
}
In this case, we have a common base Animal
that provides an implementation for the property Name
. It doesn't make sense to instantiate Animal yourself as there are no animals in the world that are just animals, they wither dogs or cats or whatever.
Here's a case where it makes sense to have a non-abstract base.
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}, ....){
}
}
Here a way that is not a subclass makes sense, we can create any arbitrary shape, but it would be more convenient to use a sublayer for more specific shapes.
a source to share
It sounds like you are a little confused about the difference between a virtual method and an abstract class. You don't need to declare your class as abstract unless you define that it doesn't make sense in it. This is useful in areas where you might have common behaviors across multiple classes.
It sounds like you want a regular class and some virtual methods.
a source to share