ุฅุญู†ุง ุงุชูƒู„ู…ู†ุง ุนู† ุงู„ู…ุจุฏุฃ ุฏู‡ ู‚ุจู„ ูƒุฏู‡ ููŠ Abstraction.

ูƒู„ู…ุฉ abstract ู‡ู†ุง ู…ุนู†ุงู‡ุง ุฅู† ุงู„ุญุงุฌุฉ ุฏูŠ ุจุชูƒูˆู† ู†ุงู‚ุตุฉุŒ ุณูˆุงุก ูƒุงู†ุช Classes and Objects in CSharp ุฃูˆ Functions in CSharp ุฃูˆ Properties in CSharp.

abstract class Shape
{
    public decimal Dim01 { get; set; }
    public decimal Dim02 { get; set; }
 
    // public decimal CalculateArea() { } 
    // I can't make like that because I don't know what is the shape
    public abstract decimal CalculateArea(); // Must be implemented by derived classes
}
 
// MAIN
// Shape sh1 = new Shape(); 
// Cannot create object (instance) - not fully implemented
// But we can create reference
Shape sh1; 

ุฃู†ุง ู‡ู†ุง ู…ุนุฑูุชุด ุฅูŠู‡ ุงู„ู„ูŠ ู‡ูŠุชุนู…ู„ ููŠ ุงู„ู€ method ุงู„ู„ูŠ ุงุณู…ู‡ุง AreaุŒ ู„ุฃู†ูŠ ู…ุด ุนุงุฑู ู‡ุณุชุฎุฏู… ุฃู†ู‡ูŠ ู‚ุงู†ูˆู†. ููƒุฏู‡ ุจู‚ุช ู†ุงู‚ุตุฉ ุนู†ุฏูŠุŒ ูˆุจุงู„ุชุงู„ูŠ ู‡ุณุชุฎุฏู… ูƒู„ู…ุฉ abstract ุนุดุงู† ุฃูˆุถุญ ุฅู†ู‡ุง ู†ุงู‚ุตุฉ ุฃูˆ ู…ุด implemented ุจุดูƒู„ ูƒุงู…ู„.

Abstract Class

ูˆุจู…ุง ุฅู† ุงู„ู€ Class ููŠู‡ ุญุงุฌุฉ ู…ุด ูƒุงู…ู„ุฉุŒ ูˆู‡ูŠ ุงู„ู€ Method ููŠ ุงู„ู…ุซุงู„ ุจุชุงุนู†ุงุŒ ูุจุงู„ุชุงู„ูŠ ุงู„ู€ Class ู†ูุณู‡ ุจูŠูƒูˆู† ู…ุด ูƒุงู…ู„. ูˆุนุดุงู† ูƒุฏู‡ ู„ุงุฒู… ู†ุณุชุฎุฏู… ู…ุนุงู‡ ูƒู„ู…ุฉ abstract.

ู…ู…ูƒู† ุจุฑุถู‡ ุงู„ู€ Class ูŠูƒูˆู† abstracted ุญุชู‰ ู„ูˆ ูƒู„ ุญุงุฌุฉ ุฌูˆุงู‡ ูƒุงู…ู„ุฉุŒ ูˆุฏู‡ ุจูŠูƒูˆู† ู„ูˆ ุฃู†ุง ุนุงูŠุฒ ุฃู…ู†ุน ุฃูŠ ุญุฏ ุฅู†ู‡ ูŠุนู…ู„ object ู…ู†ู‡ ู…ุจุงุดุฑุฉ.

ู…ูŠู†ูุนุด ุชุนู…ู„ object ู…ู† abstract class ู„ุฃู†ู‡ ุจูŠูƒูˆู† Not fully implemented (ูŠุนู†ูŠ ู†ุงู‚ุต).

ุงู„ู€ Abstract class ู…ู…ูƒู† ูŠูƒูˆู† ุนุจุงุฑุฉ ุนู† container ู„ู„ูƒูˆุฏ ู„ุญุงุฌุงุช ู‡ุชุณุชุฎุฏู…ู‡ุง ููŠ ุงู„ู…ุณุชู‚ุจู„ุŒ ุฃูˆ ุจูŠูƒูˆู† ุฒูŠ ุญุงุฌุฉ standard ู„ุญุงุฌุฉ ุชุงู†ูŠุฉ ู‡ุชูŠุฌูŠ ุจุนุฏู‡ุง ูˆู„ุณู‡ ู…ุด fully implemented.

ูƒู…ุซุงู„: ู„ูˆ ุนู†ุฏูŠ ู†ูˆุนูŠู† ู…ู† ุงู„ู€ EmployeeุŒ ูˆุงุญุฏ Partial ูˆุงู„ุชุงู†ูŠ FullyุŒ ูุงู„ู€ Employee ุงู„ุฃุจ ุฃูˆ ุงู„ุฃุณุงุณูŠ ู…ู…ูƒู† ู…ุด ู‡ุญุชุงุฌ ุฃุนู…ู„ ู…ู†ู‡ object ู…ุจุงุดุฑ.

// class created to be base class for childs
// can't intialize object from abstract class
abstract class Person
{
    public string FName { get; set; }
    public string LName { get; set; }
 
    public Person(string _FName , string _LName)
    {
        FName = _FName;
        LName = _LName;
    }
 
    public override string ToString()
    {
        return $"{FName} {LName}";
    }
}
 
// valid
Person P; 
 
// not Valid
// Person P1 = new Person("Ahmed", "Ali"); 

Example

classDiagram
    GeoShape <|-- RectBase : Inheritance
    RectBase <|-- Rect : Inheritance
    RectBase <|-- Square : Inheritance

    class GeoShape{
        +int Dim1
        +int Dim2
        +GeoShape(int D1, int D2)
        +abstract Area() double
        +abstract Perimeter() double
    }

    class RectBase{
        +RectBase(int W, int H)
        +override Area() double
    }

    class Rect{
        +override Perimeter() double
    }

    class Square{
        +override Perimeter() double
        +override Area() double
    }
abstract class GeoShape
{
    public int Dim1 { get; set; }
    public int Dim2 { get; set; }
 
    public GeoShape(int D1 , int D2) { Dim1 = D1; Dim2 = D2; }
 
    // public virtual Double Area () =0; // C++ style for pure virtual function
 
    public abstract Double Area(); 
    /// Abstract Method = Virtual + No Implementation
 
    public abstract double Perimeter { get; /*set;*/ } 
    /// Abstract ReadOnly Property
}
 
// Care, it's abstract class because 
// it inherits abstract property so it's not fully implemented
// We can make it abstract or we can implement the property
abstract class RectBase : GeoShape // : -> inherits + implements
{
    public RectBase(int W=0, int H=0) : base(W, H) { }
 
    // We will use override to implement abstract method
    // Or we can use it for override virtual method
    public override double Area() 
    {
        return Dim1 * Dim2;
    }
}
 
class Rect : RectBase
{
    // Must implement the abstract property Perimeter
    public override double Perimeter { get { return 2 * (Dim1 + Dim2); } }
}
 
class Square : RectBase
{
    /// Must
    // because perimeter didn't implemented in rectbase
    public override double Perimeter => 4 * Dim2; // Assuming Dim2 is the side length for a square based on RectBase
 
    /// Optional
    // because Area implemented in rectbase
    public override double Area()
    {
        // Could be Dim2 * Dim2 if Dim2 is the side, or call base.Area() if Dim1 and Dim2 are same
        // For demonstration, let's assume Dim1 and Dim2 are set to be equal for a square
        return Dim1 * Dim2; 
        // throw new NotImplementedException(); // Or throw if specific square logic is pending
    }
}

Abstract Class vs Interface

Interface

  • ู‡ูˆ ุนุจุงุฑุฉ ุนู† Code contract ุจูŠู† ุงู„ู€ Developer ุงู„ู„ูŠ ูƒุชุจู‡ ูˆุงู„ู€ Developer ุงู„ู„ูŠ ู‡ูŠุนู…ู„ู‡ Implementation
Link to original

Transclude of Understanding-Access-Modifiers-in-CSharp#inside-cs-interface-interface

Abstract Class

ูƒู…ุซุงู„: ู„ูˆ ุนู†ุฏูŠ ู†ูˆุนูŠู† ู…ู† ุงู„ู€ EmployeeุŒ ูˆุงุญุฏ Partial ูˆุงู„ุชุงู†ูŠ FullyุŒ ูุงู„ู€ Employee ุงู„ุฃุจ ุฃูˆ ุงู„ุฃุณุงุณูŠ ู…ู…ูƒู† ู…ุด ู‡ุญุชุงุฌ ุฃุนู…ู„ ู…ู†ู‡ object ู…ุจุงุดุฑ.

Link to original

Transclude of Understanding-Access-Modifiers-in-CSharp#inside-cs-class-class-or-cs-struct-struct

ุจูŠูƒูˆู† ู…ุชุงุญ ูƒู…ุงู† ุญุงุฌุชูŠู† ุฒูŠุงุฏุฉ:

  1. Abstract method
  2. Abstract property