Abstract classes are the base class of C#. Looks good but What is abstract keyword?
abstract class BaseClass { }
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(); }
So if you do not implement interface members in abstract class it will give you a compile time error.
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 6 - Child classes must override all abstract methods but may override virtual methods.
public abstract class BaseClass1 { public abstract void MyMethod(); } public abstract class BaseClass2 : BaseClass1 { public virtual void MyMethod2() { //some base implementation. } } public class ChildCLass : BaseClass2 { }
public class ChildCLass : BaseClass2 { public override void MyMethod() { //Some implementation } }
Point 8 - You cannot use private access modifier in case of abstract/virtual methods.
Hope you enjoyed reading this article. Drop a comment if you still have some confusion.


















