top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

What are abstract methods in C# ?

+1 vote
20 views
What are abstract methods in C# ?
posted Apr 7, 2017 by Pooja Bhanout

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

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:

abstractmethod 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.

answer Apr 17, 2017 by Shweta Singh
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...