Abstract Class in C#

Introduction

In this blog we described about abstract class in C#.

About Abstract Class

Abstract class is a special kind of class that provides the default functionality to their subclasses. Abstract class can not me instantiated means we can not create the instance of abstract class but abstract class can be implemented in their subclasses. Abstract classes can have abstract functions or non abstract functions. a abstract class which have a "abstract" keyword in a function it does not provide the implementation of that function in abstract class. The implementation and logic of abstract method is provided in subclasses. Subclasses use "override" keyword with same method name (as abstract method name) to provide further implementation of abstract methods. Use the Abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Features of Abstract Class

  • An abstract class can not be instantiated.
  • An abstract class may contain abstract method and non abstract methods.
  • Abstract classes are designed to be used as base classes for other abstract and non-abstract classes.

Abstract Method

Abstract methods are part of abstract class. If the abstract class contains any methods with keyword as abstract then such methods are termed as abstract methods. When a class inherits from an abstract class, the derived class must implement all the abstract methods of the abstract class.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

public abstract int multiplication(int num1, int num2); //abstract method

Features of Abstract Method

  • An abstract method must be override in child classes.
  • An abstract method does not have implementation detail in abstract class.
  • Only abstract class can have abstract method.

using System; 

namespace ConsoleApplication1

{

     abstract class myclass

{

        public int addfunction(int num1,int num2) //non abstract method

         {

             return (num1 + num2);

         }

        public abstract int multiplication(int num1, int num2); //abstract method       

}

    class Program:myclass

    {

        public override int multiplication(int n1, int n2)

        {

            return (n1 * n2);

        } 

        static void Main(string[] args)

        {

            Program obj = new Program();

            Console.WriteLine("Enter First Number:");

            int n1 = Convert.ToInt16(Console.ReadLine());

            Console.WriteLine("Enter Second Number:");

            int n2 = Convert.ToInt16(Console.ReadLine());

            int add = obj.addfunction(n1, n2);

            Console.WriteLine("Addition is {2} of First Number{0}, Second Number {1}" , n1,n2, add);

            Console.ReadLine();

            int mul = obj.multiplication(n1, n2);

            Console.WriteLine("Multiplication is {2} of First Number {0}, Second Number {1}", n1, n2, mul);

            Console.ReadLine(); 

        }

    }

}

Output

Pass two numbers as shown below.

FIG1.jpg

Result of Addition & Multiplication of numbers.

FIG2.jpg

Summary

An abstract class is a class that you cannot create an instance of. It can provide basic functionality, but in order for that functionality to be used, one or more other classes must derive from the abstract class. One of the major benefits of abstract classes is that you can reuse code without having to retype it.