Abstract Class And Abstract Method In C#

Introduction

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:

abs1.jpg

and our code window looks like,

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication13 {  
  6.     abstract class mcn {  
  7.         public int add(int a, int b) {  
  8.             return (a + b);  
  9.         }  
  10.     }  
  11.     class mcn1: mcn {  
  12.         public int mul(int a, int b) {  
  13.             return a * b;  
  14.         }  
  15.     }  
  16.     class test {  
  17.         static void Main(string[] args) {  
  18.             mcn1 ob = new mcn1();  
  19.             int result = ob.add(5, 10);  
  20.             Console.WriteLine("the result is {0}", result);  
  21.         }  
  22.     }  
  23. }  

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,

abs2.jpg

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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ConsoleApplication14 {  
  6.     abstract class test1 {  
  7.         public int add(int i, int j) {  
  8.             return i + j;  
  9.         }  
  10.         public abstract int mul(int i, int j);  
  11.     }  
  12.     class test2: test1 {  
  13.         public override int mul(int i, int j) {  
  14.             return i * j;  
  15.         }  
  16.     }  
  17.     class test3: test1 {  
  18.         public override int mul(int i, int j) {  
  19.             return i - j;  
  20.         }  
  21.     }  
  22.     class test4: test2 {  
  23.         public override int mul(int i, int j) {  
  24.             return i + j;  
  25.         }  
  26.     }  
  27.     class myclass {  
  28.         public static void main(string[] args) {  
  29.             test2 ob = new test4();  
  30.             int a = ob.mul(2, 4);  
  31.             test1 ob1 = new test2();  
  32.             int b = ob1.mul(4, 2);  
  33.             test1 ob2 = new test3();  
  34.             int c = ob2.mul(4, 2);  
  35.             Console.Write("{0},{1},{2}", a, b, c);  
  36.             Console.ReadLine();  
  37.         }  
  38.     }  
  39. }  

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.

Up Next
    Ebook Download
    View all
    Learn
    View all