Abstract Base Class or Abstraction

Abstract Base Class or Abstraction:

Introduction

An Abstract Base class cannot be instantiated; it means the object of that class cannot be created. The class having the abstract keyword with some of its methods (not all) is known as an Abstract Base Class. The class having the Abstract keyword with all its methods is a pure Abstract Base Class.

The method of the abstract class that has no implementation is known as "operation". It can be defined as an abstract void method ();

An abstract class holds the methods, but the actual implementation is made in the derived class.

Example 1

class program
{
    abstract class animal
    {
        public abstract void eat();
        public void sound()
        {
            Console.WriteLine("dog can sound");
        }
    }
    class dog : animal
    {
        public override void eat()
        {
            Console.WriteLine("dog can eat");
        }
    }
    static void Main(string[] args)
    {
        dog mydog = new dog();
        animal thePet = mydog;
        thePet.eat();
        mydog.sound();
    }
}

Example 2

// C# program to calculate the area 
// of a square using the concept of
// data abstraction
using System;
 
namespace Demoabstraction {
     
// abstract class
abstract class Shape {
 
    // abstract method
    public abstract int area();
}
 
// square class inheriting
// the Shape class
class Square : Shape {
 
    // private data member
    private int side;
 
    // method of  square class
    public Square(int x = 0)
    {
        side = x;
    }
     
    // overriding of the abstract method of Shape
    // class using the override keyword
    public override int area()
    {
        Console.Write("Area of Square: ");
        return (side * side);
    }
}
 
// Driver Class
class GFG {
     
    // Main Method
    static void Main(string[] args)
    {
         
        // creating reference of Shape class
        // which refer to Square class instance
        Shape sh = new Square(4);
         
        // calling the method
        double result = sh.area();
         
        Console.Write("{0}", result);
  
    }
}
}

Example 3

using System;

namespace Demoabstraction
{
    // abstract class
    abstract class MobilePhones
    {
        // abstract methods
        public abstract string Calling();
        public abstract string Message();
    }

    class Nokia : MobilePhones
    {
        public override string Calling()
        {
            return "demo";
        }

        public override string Message()
        {
            return "demo1";
        }

        public void Sound()
        {
            Console.WriteLine("bass sound");
        }
    }

    class OnePlus : MobilePhones
    {
        public override string Calling()
        {
            return "demo";
        }

        public override string Message()
        {
            return "demo1";
        }

        public void Camera()
        {
            Console.WriteLine("camera");
        }
    }
}