Abstract Class And Abstract Method In C#

In this article I am going to explain abstract classes and abstract methods. With the help of abstraction we can achieve dynamic polymorphism i.e. the same method name in different classes but the same signature.

Abstract class

If a class is defined as abstract then we can't create an instance of that class. By the creation of the derived class object where an abstract class is inherit from, we can call the method of the abstract class.

Let's take an example: First of all, select a console application as follows:

and our code window looks like,

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication13 {

abstractclass mcn {

publicint add(int a, int b) {

return (a + b);

}

}

class mcn1: mcn {

publicint mul(int a, int b) {

return a * b;

}

}

class test {

staticvoid Main(string[] args) {

mcn1 ob = new mcn1();

int result = ob.add(5, 10);

Console.WriteLine("the result is {0}", result);

}

}

}

In the above program we can call the method of the abstract class mcn with the help of an object of the mcn1 class which inherits from the class mcn. When we run the above program the output is the addition of 5 & 10 (i.e. 15) which is shown as,

Abstract method

An Abstract method is a method without a body. The implementation of an abstract method is done by a derived class. When the derived class inherits the abstract method from the abstract class, it must override the abstract method. This requirment is enforced at compile time and is also called dynamic polymorphism.

The syntax of using the abstract method is as follows:

<access-modifier>abstract<return-type>method name (parameter)

The abstract method is declared by adding the abstract modifier the method.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication14 {

abstractclass test1 {

publicint add(int i, int j) {

return i + j;

}

publicabstractint mul(int i, int j);

}

class test2: test1 {

public override int mul(int i, int j) {

return i * j;

}

}

class test3: test1 {

public override int mul(int i, int j) {

return i - j;

}

}

class test4: test2 {

public override int mul(int i, int j) {

return i + j;

}

}

class myclass {

publicstaticvoid main(string[] args) {

test2 ob = new test4();

int a = ob.mul(2, 4);

test1 ob1 = new test2();

int b = ob1.mul(4, 2);

test1 ob2 = new test3();

int c = ob2.mul(4, 2);

Console.Write("{0},{1},{2}", a, b, c);

Console.ReadLine();

}

}

}

In the above program, one method i.e. mul can perform various functions depending on the value passed as parameters by creating an object of various classes which inherit other classes. Hence we can acheive dynamic polymorphism with the help of an abstract method.

NOTE

When we use an override keyword the preference will be given to classes which has an instance created; in case of inheritance, the preference will provide to the reference variable class.