What is an Abstract class?
The class which is declared using abstract keyword is an abstract class. An abstract class cannot be initialized, it can only be inherited by another class. Abstract consists of abstract methods and can also consist of an instance method. Only methods, properties ,and indexers can be abstract, fields cannot be abstract.
Constructor calls the sequence when any class derived from abstract class is initialized and is the same as the normal class as shown below,
- Static constructor of the base class is called.
- Static constructor of the derived class is called.
- Instance constructor base class is called.
- Instance constructor derived class is called.
The class which is inheriting abstract class must provide the implementation of abstract properties, indexers and methods.
Example of abstract properties,
- abstract class Employee {
- public abstract string firstName {
- get;
- set;
- }
- }
- class Manager: Employee {
- private string _firstName;
- public override string firstName {
- set {
- this._firstName = value;
- }
- get {
- return this._firstName;
- }
- }
- }
Inheritance Chaining and Abstract class
In inheritance chaining, if there are three classes and they consist of methods which override at every level of inheritance it looks as shown below,
Part 1
- class A {
- public virtual void Method1() {
- Console.WriteLine("Class A Method1");
- }
- }
- class B: A {
- public override void Method1() {
- Console.WriteLine("Class B Method1");
- }
- }
- class C: B {
- public override void Method1() {
- Console.WriteLine("Class C Method1");
- }
- }
Madan ShekarPosted Oct 18, 2018, 2:29 AM
Nice article
Rushi MehtaPosted Oct 16, 2018, 10:55 PM
Nice Article