C# Abstraction with Examples

Data abstraction is a resource based on the most important characteristics of the user. No unnecessary detail is shown to the user.

In C# we can achieve the abstraction with the help of abstract class

  • We can declare the abstract class with the keyword of abstract
  • It is not allowed to create abstract class objects in C #. Or in other words, you can not use the abstract class directly with the new operator.
  • The class that contains the abstract keyword along with some of the methods (not all abstract methods) is known as the abstract base class.
  • The class that contains the abstract keyword with all its methods is called a pure abstract base class.
  • You cannot explain abstract methods outside the abstract class.
  • You cannot declare an abstract class as a sealed class.

Example:

abstract class Shape_Class
    {
        public abstract int Total_area();
    }
    class Square_Class : Shape_Class
    {
        private int side_length; 
        public Square_Class(int x = 0)
        {
            side_length = x;
        }
        public override int Total_area()
        {
            Console.Write("Total area of Square Class: ");
            return (side_length * side_length);
        }
    }
    class EntryPoint
    {
        static void Main(string[] args)
        {
            Shape_Class sh = new Square_Class(10);
            double result = sh.Total_area();
            Console.Write(result);

        }
    }

Output:

Total area of Square Class: 100

Abstract Class

The word abstract known as a concept or an idea not associated with any specific instance. In programming, we apply the same meaning of abstraction by making classes not associated with any specific instance

In C# abstraction is achieved by the abstract classes and interface

Abstract Method

A method called an abstract method that has no "body" and is only described in the summary class.