Abstract classes are the base class of C#. Looks good but What is abstract keyword?
So abstract is a modifier that can be used with classes, indexers, properties, methods and events.
Let's talk about Abstract Class first - abstract class BaseClass { }
Using abstract keyword we can create abstract class. These classes are always implemented in inheritance model. Some points need to remember before implementing it -
1. Abstract classes cannot be instantiated.
2. Abstract class may or may not contain abstract methods.
3. Abstract classes can inherit interfaces and that class should implement all interface members.
4. Abstract class can have public, protected constructor but still you cannot instantiate it.
5. Abstract class can inherit Abstract class but need not to implement/override any abstract method .
6. Child class must override all abstract methods but may override virtual methods.
7. Abstract methods cannot mark as virtual.
8. You cannot use private access modifier in case of abstract/virtual methods.
Point 1 - Abstract classes cannot be instantiated.
See this -
An Abstract keyword is define in a way you can create only subtype of that. This is how framework designed abstract keyword. But if I say you can create an instance of abstract class. Confused ??
public MainClass() { BaseClass obj = new ChildClass(); }
Yes Base class can be instantiated through child class only.
Point 2 - Abstract classes may or may not contain abstract methods.
As code mentioned in above example my abstract class doesn't have any abstract method. I do not get any compile time error for the same.
Point 3 - Abstract classes may can inherit interfaces and that class should implement all interface members
So if you do not implement interface members in abstract class it will give you a compile time error.
Point 4 - Abstract class can have public, protected or private constructor but still you cannot instantiate it.
public abstract class BaseClass { public BaseClass() { } private BaseClass(string name) { } protected BaseClass(string name, string instantiatedFor) { } void MYMethod() { } }
After defining these constructors you still cannot instantiate it by default.So what is the use case of defining these constructors in the abstract class ?
Child classes can pass some data to base class to initialize it. Let's say child classes want to initialize base
class with their name to track the object.
Point 5 - Abstract class can inherit Abstract class.
Yes Abstract class can inherit abstract class but they need not to override any method defined in super base class. See below image for more understanding.
Point 6 - Child classes must override all abstract methods but may override virtual methods.
See the below example for more understanding -
public abstract class BaseClass1 { public abstract void MyMethod(); } public abstract class BaseClass2 : BaseClass1 { public virtual void MyMethod2() { //some base implementation. } } public class ChildCLass : BaseClass2 { }
So in case you have some base implementation and you do not want to override it in child class then mark it as virtual so marking it virtual you are explaining to CLI. You may or may not override this method. So to fix this error -
public class ChildCLass : BaseClass2 { public override void MyMethod() { //Some implementation } }
Point 7 - Abstract methods cannot mark as virtual.
See the compile time error for more understanding
Point 8 - You cannot use private access modifier in case of abstract/virtual methods.
See below screenshot for more understanding -
Hope you enjoyed reading this article. Drop a comment if you still have some confusion.
Happy Coding :)