Abstract Methods and Class in C# 

We have to do an abstract class as well because, in this type of class, different types of methods are used to Declare, but they are not implemented or define. Rather, they are left to implement in those Derived Classes, which are inherited with this type of Abstract Classes.

Abstract Methods and Class in C#

A class declared abstract may or may not include abstract methods. They are created with the purpose of being a superclass.

An abstract class can have constructors—this is one major difference between an abstract class and an interface.

Abstract Methods and Class in C#

When we create a method without a body is known as an abstract method, what the method contains the only declaration without any implementation & should be declared by only the ‘abstract modifier’.

It is mandatory to override an abstract method in the derived class.

These abstract classes and methods are declared with the 'abstract' keyword. An abstract class can only be extended, and cannot be directly instantiated.

Abstract Methods and Class in C#

The abstract class provides a little more than interfaces. The Interfaces do not include fields and superclass methods that get inherited, whereas abstract classes do. This means that an abstract class is more closely related to a class which extends it than an interface is to a class that implements it.

The abstract methods are must be implemented in the non-abstract class using the override keyword. 

Features:

  • The abstract class can inherit from a class and one or more interfaces.
  • The abstract class can implement code with non-Abstract methods.
  • The Abstract class can have modifiers for methods, properties etc.
  • The Abstract class can have constants and fields.
  • The abstract class can implement a property.
  • The abstract class can have constructors or destructors.
  • The abstract class cannot be inherited from by structures.
  • The abstract class cannot support multiple inheritances.
The syntax of using the abstract method is as follows :

<access-modifier>abstract<return-type>method name (parameter)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication24
{
    class myclass
    {
        public static void Main(string[] args)
        {
            demo2 ob = new demo4();
            int a = ob.mul(2, 4);
            demo1 ob1 = new demo2();
            int b = ob1.mul(4, 2);
            demo1 ob2 = new demo3();
            int c = ob2.mul(4, 2);
            Console.Write("{0},{1},{2}", a, b, c);
            Console.ReadLine();
        }
    }

    abstract class demo1
    {
        public int add(int i, int j)
        {
            return i + j;
        }
        public abstract int mul(int i, int j);
    }
    class demo2 : demo1
    {
        public override int mul(int i, int j)
        {
            return i * j;         }
    }
    class demo3 : demo1
    {
        public override int mul(int i, int j)
        {
            return i - j;
        }
    }
    class demo4 : demo2
    {
        public override int mul(int i, int j)
        {
            return i + j;
        }
    }
}

Abstract Methods and Class in C#

Abstract Methods and Class in C#


If you want to know more about to abstract classes and methods then follow these links :

1). https://www.mindstick.com/articles/12035/abstract-class-in-c-sharp

2) https://www.mindstick.com/articles/11956/difference-between-interface-vs-abstract-class-in-c-sharp

3). https://www.mindstick.com/articles/1430/abstract-class

4). https://www.mindstick.com/articles/952/abstraction-in-c-sharp-with-example

5). https://www.mindstick.com/articles/92/static-and-abstract-class


Thank You for reading this article, and if you have any suggestion, Please comment in the comment box.


  Modified On Nov-16-2018 06:20:23 AM
  1. Following statements should give you more clarity:

    • The class must be either abstract or concrete.
    • Interface, by definition, is abstract.
    • Neither interface nor abstract class can be instantiated.
    • The class may inherit from zero or one (abstract or concrete) class.
    • The class may implement zero or more interfaces.
    • The interface may inherit from zero or more interfaces.
    • The interface cannot inherit a class.
    • An abstract class may have members with implementation.
    • An interface cannot have any member with implementation.
    • To be concrete, a class must implement all members in the interface.

    I hope this helps.

  1. It's according to my own experience :

    After reading your article, There is no doubt that your article is very good.

    And you have done a great job for it.

    Abstract Function:

    • It can be declared only inside the abstract class.
    • It contains only method declaration not the implementation in the abstract class.
    • It must be overridden in the derived class.

    Virtual Function:

    • It can be declared inside abstract as well as nonabstract class.
    • It contains method implementation.
    • It may be overridden.

Leave Comment