ุฅุญูุง ุงุชููู ูุง ุนู ุงูู ุจุฏุฃ ุฏู ูุจู ูุฏู ูู 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
Link to original
- ูู ุนุจุงุฑุฉ ุนู Code contract ุจูู ุงูู Developer ุงููู ูุชุจู ูุงูู Developer ุงููู ููุนู ูู Implementation
Transclude of Understanding-Access-Modifiers-in-CSharp#inside-cs-interface-interface
Abstract Class
ูู ุซุงู: ูู ุนูุฏู ููุนูู ู ู ุงูู
Link to originalEmployeeุ ูุงุญุฏPartialูุงูุชุงููFullyุ ูุงููEmployeeุงูุฃุจ ุฃู ุงูุฃุณุงุณู ู ู ูู ู ุด ูุญุชุงุฌ ุฃุนู ู ู ููobjectู ุจุงุดุฑ.
Transclude of Understanding-Access-Modifiers-in-CSharp#inside-cs-class-class-or-cs-struct-struct
ุจูููู ู ุชุงุญ ูู ุงู ุญุงุฌุชูู ุฒูุงุฏุฉ:
Abstract methodAbstract property