Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derive abstract class from non-abstract class

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.

like image 300
Jehof Avatar asked Apr 08 '10 19:04

Jehof


People also ask

Can an abstract class be derived from a non abstract class?

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.

Can abstract class be derived?

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.

Can a class inherit from a non abstract class?

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.

Can we have an abstract method inside non abstract class and vice versa?

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.


2 Answers

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 -- abstract
Brother: Sibling -- concrete
Sister: Sibling -- concrete

like image 160
Dan Tao Avatar answered Oct 09 '22 10:10

Dan Tao


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.

like image 26
leppie Avatar answered Oct 09 '22 12:10

leppie