Abstraction in C#

Abstraction defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details.
The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.

Example: 
Consider a real-life scenario of withdrawing money from ATM. 
The user only knows that in ATM machine first enter ATM card, then enter the pin code of ATM card, and then enter the amount which he/she wants to withdraw and at last, he/she gets their money. 
The user does not know about the inner mechanism of the ATM or the implementation of withdrawing money etc. 
The user just simply know how to operate the ATM machine, this is called abstraction.

In C#, abstraction is achieved with the help of Abstract classes.
Abstract Classes
-An abstract class is declared with the help of abstract keyword.
-In C#, you are not allowed to create objects of the abstract class. Or in other words, you cannot use the abstract class directly with the new operator.
-Class that contains the abstract keyword with all of its methods is known as pure Abstract Base Class.
-You are not allowed to declare the abstract methods outside the abstract class.
-You are not allowed to declare abstract class as Sealed Class.

There are situations in which we want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. 
That is, sometimes we want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.
Consider a classic “shape” example, perhaps used in a computer-aided design system or game simulation. 
The base type is “shape” and each shape has a color, size and so on. 
From this, specific types of shapes are derived(inherited)-circle, square, triangle and so on – each of which may have additional characteristics and behaviors. 

The following are some of the key points −
You cannot create an instance of an abstract class.
You cannot declare an abstract method outside an abstract class.
When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.


Example

using System;
namespace Demo {
   abstract class Shape {
      public abstract int area();
   }
   
   class Rectangle:  Shape {
      private int length;
      private int width;
      
      public Rectangle( int a = 0, int b = 0) {
         length = a;
         width = b;
      }
      public override int area () { 
         Console.WriteLine("Rectangle class area :");
         return (width * length); 
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(20, 15);
         double a = r.area();
         Console.WriteLine("Area: {0}",a);
         Console.ReadKey();
      }
   }
}

Output-
Rectangle class area :
Area: 300

No comments:

Post a Comment