When we can use an abstract class ?

 

The purpose of an abstract class is to provide some ancestral behaviors while also defining methods that must be implemented by sub-classes. 

As we know, an abstract class can have both abstract and non-abstract methods, but there is no strict compiler rule that forces an abstract class to contain both abstract and non-abstract methods. But the programming principles say, an abstract class must contain both abstract and non-abstract methods, if it is not then you can use a normal class or an interface instead of the abstract class.


When not to use an abstract class?

An abstract class with no abstract methods that were made abstract purely to prevent instantiation should be converted to a concrete class with a protected constructor, for an example:


An abstract class with only abstract methods and no inheritable behavior should be converted to an interface, for an example:



An abstract class should have both abstract and concrete methods, for an example:

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form