Reader Level:
Articles

Abstract Class and Abstract Method in C#

By Manish Sharma on May 21, 2012
In this article I have described abstraction in the C# language and a way to achieve dynamic polymorphism with the help of abstract method in the C# language.
  • 0
  • 0
  • 31459

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:

using
System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication13
{
abstract
class mcn
{
   public
int add(int a, int b)
    {
      return
(a + b);
    }
}
class
mcn1 : mcn
{
  public
int mul(int a, int b)
  {
   return
a * b;
  }
}
class
test
{
  static
void 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:

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication14
{
  abstract
class test1
 
{
    public
int add(int i, int j)
    {
      return
i+j;
    }
    public
abstract int 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
 
{
  public
static void 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.

COMMENT USING

Trending up