Is it OK to derive an abstract class from a non-abstract class or is there something wrong with this approach?
Here´s a little example:
public class Task { // Some Members } public abstract class PeriodicalTask : Task { // Represents a base class for task that has to be done periodicaly. // Some additional Members } public class DailyTask : PeriodicalTask { // Represents a Task that has to be done daily. // Some additional Members } public class WeeklyTask : PeriodicalTask { // Represents a Task that has to be done weekly. // Some additional Members }
In the example above I do not want to make the class Task abstract, because I want to instantiate it directly. PeriodicalTask should inherit the functionality from Task and add some additional members but I do not want to instantiate it directly. Only derived class of PeriodicalTask should be instantiated.
An abstract method must be implemented in all non-abstract classes using the override keyword. After overriding, the abstract method is in the non-Abstract class. We can derive this class in another class, and again we can override the same abstract method with it.
Absolutely, an Abstract class can inherit from both non-abstract classes and other abstract classes. When you want any class to inherit from another class, you will want to watch out (most of the time) for the sealed modifier.
Yes :) Unless it has the final modifier. Show activity on this post. No it doesn't need to have the word abstract, the word abstract just wont allow you to create an instance of that class directly, if you use the word abstract you can only create an instance of the classes that extend that abstract class.
One feature of abstract classes is that you can have implemented base class functionality in addition to abstract methods that must be implemented. This can be beneficial for code reuse. There can not be abstract methods in non abstract classes.
I don't see anything wrong with this approach.
You might have some basic type that can be described in concrete terms. Now, just because an object of this type might be further classified according to some subtype, it does not follow that all such subtypes are just as concrete; they may in turn require further concretization, as it were.
Real-world example:
Person
-- concrete (non-abstract)Sibling: Person
-- abstractBrother: Sibling
-- concreteSister: Sibling
-- concrete
Nothing wrong with it.
If you look at a big hierarchy like WinForms, you will find several layers of abstract types.
MSBuild tasks are also a good (and more relevant) example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With