Introduction

ููŠ ุงู„ู…ู‚ุงู„ ุฏู‡ุŒ ู‡ู†ุชูƒู„ู… ุนู† ุงู„ู€ Abstract Classes ูˆุงู„ู€ Methods ููŠ C# ุจุงู„ุฃู…ุซู„ุฉ. ูŠุง ุฑูŠุช ุชู‚ุฑุง ู…ู‚ุงู„ู†ุง ุงู„ู„ูŠ ูุงุช ุงู„ู„ูŠ ุงุชูƒู„ู…ู†ุง ููŠู‡ ุนู† ุงู„ู€ Generalization ูˆุงู„ู€ Specialization ููŠ C#.

What are Abstract Methods in C#?

ููŠ C#ุŒ ุงู„ู€ abstract methods ู‡ูŠ methods ุจู†ุนุฑูู‡ุง ุฌูˆู‡ abstract class ุฃูˆ interface ุจุณ ู…ุจุชูƒูˆู†ุด ู„ูŠู‡ุง body ุฃูˆ implementation ููŠ ุงู„ู€ class ุฃูˆ ุงู„ู€ interface ุงู„ู„ูŠ ุจูŠุนุฑูู‡ุง. ุจุฏู„ ูƒุฏู‡ุŒ ู…ุณุคูˆู„ูŠุฉ ุชู†ููŠุฐ ุงู„ู€ method ุฏูŠ ุจุชุชู†ู‚ู„ ู„ุฃูŠ concrete (non-abstract) class ุจูŠูˆุฑุซ ู…ู† ุงู„ู€ abstract class ุฏู‡ ุฃูˆ ุจูŠู†ูุฐ ุงู„ู€ interface ุฏู‡.

ุงู„ู€ method ุงู„ู„ูŠ ู…ู† ุบูŠุฑ body ุจุชุชุนุฑู ุนู„ู‰ ุฅู†ู‡ุง Abstract Method. ุงู„ู„ูŠ ุงู„ู€ method ุจูŠุญุชูˆูŠู‡ ู‡ูˆ ู…ุฌุฑุฏ (declaration). ุฏู‡ ู…ุนู†ุงู‡ ุฅู† ุงู„ู€ abstract method ุจุชุญุชูˆูŠ ุนู„ู‰ ุชุนุฑูŠูู‡ุง ุจุณุŒ ู…ู† ุบูŠุฑ implementation.

ุงู„ู€ method ุงู„ู„ูŠ ุฌุงูŠุฉ ุฏูŠ non-abstract ู„ุฅู†ู‡ุง ุจุชุญุชูˆูŠ ุนู„ู‰ body:

public void Add(int num1, int num2)
{
    // Method body
}

ู„ูƒู† ู…ู† ุบูŠุฑ ู…ุง ู†ูƒุชุจ method bodyุŒ ู„ูˆ ู†ู‡ูŠู†ุง ุงู„ู€ method ุจู€ semicolon (;) ุฒูŠ ูƒุฏู‡ุŒ ุจุชุชุณู…ู‰ Abstract Method:

public void Add(int num1, int num2);

ู„ูƒู† ุงูุชูƒุฑุŒ ู„ูˆ ุนุงูŠุฒ ุชุฎู„ูŠ ุฃูŠ method ุชุจู‚ู‰ abstract methodุŒ ู„ุงุฒู… ุชุณุชุฎุฏู… ุงู„ู€ abstract modifier ุจุดูƒู„ ุตุฑูŠุญ ูƒุงู„ุชุงู„ูŠ. ูˆุจู…ุฌุฑุฏ ู…ุง ุชุณุชุฎุฏู… ุงู„ู€ abstract modifierุŒ ุงู„ู€ method ู‡ุชุชุณู…ู‰ abstract method ุชู„ู‚ุงุฆูŠู‹ุง.

public abstract void Add(int num1, int num2);

ุทูŠุจุŒ ุฅูŠู‡ ู‡ูŠ ุงู„ู€ abstract classesุŸ

What are Abstract Classes in C#?

ููŠ C#ุŒ ุงู„ู€ abstract class ู‡ูˆ class ุจูŠุดุชุบู„ ูƒู€ blueprint (ู…ุฎุทุท) ู„ู„ู€ classes ุงู„ุชุงู†ูŠุฉ. ุงู„ู€ Abstract classes ู…ูŠู†ูุนุด ู†ุนู…ู„ู‡ุง instantiated ุจุดูƒู„ ู…ุจุงุดุฑ (ูŠุนู†ูŠ ู…ูŠู†ูุนุด ู†ุนุฑู ู…ู†ู‡ุง Object ุจุดูƒู„ ู…ุจุงุดุฑ)ุŒ ู„ูƒู† ู…ู…ูƒู† ู†ุณุชุฎุฏู…ู‡ุง ูƒู€ base classes ู„ู„ู€ classes ุงู„ุชุงู†ูŠุฉ ุงู„ู„ูŠ ุจุชูˆุฑุซ ู…ู†ู‡ุง. ุงู„ู€ Abstract classes ุจูŠุชู… ุชุนุฑูŠูู‡ุง ุจุงุณุชุฎุฏุงู… ุงู„ู€ abstract keyword. ุบุงู„ุจู‹ุง ุจุชุนุฑู‘ู ู…ุฌู…ูˆุนุฉ ู…ุดุชุฑูƒุฉ ู…ู† ุงู„ุฎุตุงุฆุต ุฃูˆ ุงู„ุณู„ูˆูƒูŠุงุช ุงู„ู„ูŠ ุงู„ู…ูุฑูˆุถ ุชูƒูˆู† ู…ุดุชุฑูƒุฉ ุจูŠู† ุฃูƒุชุฑ ู…ู† derived class.

ุงู„ู€ class ุงู„ู„ูŠ ุจู†ุนุฑู‘ู ุฌูˆุงู‡ abstract methods ุจูŠุชุนุฑู ุนู„ู‰ ุฅู†ู‡ abstract class. ุญุณุจ OOPุŒ ุฅุญู†ุง ู…ุญุชุงุฌูŠู† ู†ุนุฑู‘ู ุงู„ู€ method ุฌูˆู‡ class. ู…ู†ู‚ุฏุฑุด ู†ุนุฑู‘ู abstract methods ููŠ ุฃูŠ ู…ูƒุงู† ุจุดูƒู„ ู…ุจุงุดุฑ. ู„ุงุฒู… ู†ุนุฑู‘ู ุงู„ู€ abstract method ุฌูˆู‡ abstract class ุจุณ.

ุงูุชุฑุถ ุฅู†ู†ุง ู„ุงุฒู… ู†ูƒุชุจ ุงู„ู€ Add abstract method ุงู„ู„ูŠ ูุงุชุช ุฌูˆู‡ Calculator class. ุณุงุนุชู‡ุงุŒ ุงู„ู€ class ุฏู‡ ู„ุงุฒู… ูŠุชู… ุชุนุฑูŠูู‡ ุจุงุณุชุฎุฏุงู… ุงู„ู€ abstract modifier ูƒุงู„ุชุงู„ูŠ:

public abstract class Calculator
{
    public abstract void Add(int num1, int num2);
}

ูู„ูˆ class ุฌูˆุงู‡ุง ุฃูŠ abstract methodsุŒ ู„ุงุฒู… ูˆุญุชู…ู‹ุง ูŠุชู… ุงู„ุฅุนู„ุงู† ุนู†ู‡ ุจุงุณุชุฎุฏุงู… ุงู„ู€ abstract modifierุŒ ูˆู„ู…ุง class ุจูŠุชุนู…ู„ ุจุงุณุชุฎุฏุงู… abstract modifierุŒ ุจูŠุชุณู…ู‰ Abstract class ููŠ C#.

ุนู…ูˆู…ู‹ุงุŒ ู„ู…ุง ุจู†ุนุฑู‘ู abstract classุŒ ุจูŠุฌูŠู„ู†ุง ุดูƒ: ู…ู† ุบูŠุฑ method bodyุŒ ุฅูŠู‡ ู‡ุชูƒูˆู† ูุงูŠุฏุฉ ุงู„ู€ method ุฏูŠุŸ ุฎู„ูˆู†ุง ู†ูู‡ู… ุฏู‡.

What is the use of the Abstract Method in C#?

ู„ูˆ method ุชู… ุงู„ุฅุนู„ุงู† ุนู†ู‡ุง ูƒู€ abstract ุชุญุช ุฃูŠ classุŒ ูŠุจู‚ู‰ ุงู„ู€ child class ุจุชุงุน ุงู„ู€ abstract class ุฏู‡ ู…ุณุคูˆู„ ุนู† ุชู†ููŠุฐ (implementing) ุงู„ู€ abstract method ุฏูŠ ู…ู† ุบูŠุฑ ุฃูŠ ุชู‚ุตูŠุฑ.

ููŠ ุงู„ู€ inheritanceุŒ ุดูู†ุง ุฅู† ุงู„ู€ Parent class ุจูŠูˆูุฑ ุดูˆูŠุฉ ุฎุตุงุฆุต ู„ู„ู€ Child class ุนุดุงู† ูŠุณุชุฎุฏู…ู‡ุง. ู‡ู†ุง ุจุฑุถู‡ ุงู„ู€ inheritance ุจูŠุฏุฎู„ ููŠ ุงู„ุตูˆุฑุฉุŒ ู„ูƒู† ุงู„ู†ู‚ุทุฉ ุงู„ู„ูŠ ู„ุงุฒู… ุชูุชูƒุฑู‡ุง ู‡ูŠ ุฅู† ุงู„ู€ Parent class ุจูŠูƒูˆู† abstractุŒ ูˆู‡ูˆ ู…ุด ู‡ูŠูˆูุฑ ุฃูŠ ุฎุตุงุฆุต ู„ู„ู€ Child class ุนุดุงู† ูŠุณุชุฎุฏู…ู‡ุง. ุจุงู„ุนูƒุณุŒ ู‡ูˆ ุจูŠูุฑุถ ุดูˆูŠุฉ ู‚ูŠูˆุฏ (restrictions) ุนู„ู‰ ุงู„ู€ Child classes. ูˆุงู„ุฃุจู†ุงุก ุฃูˆ ุงู„ู€ Child classes ู„ุงุฒู… ูŠุชุจุนูˆุง ุฃูˆ ูŠูˆููˆุง ุจุงู„ู‚ูŠูˆุฏ ุฏูŠ. ูˆุฏูŠ ู‡ูŠ ุงู„ููƒุฑุฉ ุงู„ุฃุณุงุณูŠุฉ ู„ู„ู€ abstract class ููŠ C#.

ู…ู„ุงุญุธุฉ: ูƒู„ abstract method ุจูŠุชู… ุงู„ุฅุนู„ุงู† ุนู†ู‡ุง ุฌูˆู‡ abstract class ู„ุงุฒู… ูˆุญุชู…ู‹ุง ูŠุชู… ุชู†ููŠุฐู‡ุง ู…ู† ุฎู„ุงู„ ุงู„ู€ Child classes ู…ู† ุบูŠุฑ ุฃูŠ ุชู‚ุตูŠุฑุ› ูˆุฅู„ุงุŒ ู‡ูŠุฌูŠู„ู†ุง compile-time error.

Is Abstract Class Containing Only Abstract Methods in C#?

ู…ุชูุชูƒุฑุด ุฅู† abstract class ู…ู…ูƒู† ูŠุญุชูˆูŠ ุนู„ู‰ abstract methods ุจุณ. ู‡ูˆ ู…ู…ูƒู† ูŠุญุชูˆูŠ ูƒู…ุงู† ุนู„ู‰ non-abstract methods. ู„ุงุฒู… ุชูุชูƒุฑ ุฅู† ู„ูˆ class ูƒุงู† non-abstractุŒ ูู‡ูˆ ุจูŠุญุชูˆูŠ ุนู„ู‰ non-abstract methods ุจุณุŒ ู„ูƒู† ู„ูˆ class ูƒุงู† abstractุŒ ูู‡ูˆ ู…ู…ูƒู† ูŠุญุชูˆูŠ ุนู„ู‰ ุงู„ุงุชู†ูŠู†ุŒ abstract ูˆ non-abstract methods ููŠ C#.

Who will Provide the Implementation of Abstract Methods in C#?

ุงู„ุฅุฌุงุจุฉ ู‡ูŠ ุงู„ู€ Child Class. ู„ูˆ ุนู†ุฏูƒ child class ู„ู€ abstract classุŒ ูู…ุณุคูˆู„ูŠุฉ ุงู„ู€ child class ุฅู†ู‡ ูŠูˆูุฑ ุงู„ู€ implementation ู„ูƒู„ ุงู„ู€ abstract methods ุจุชุงุนุฉ ุงู„ู€ parent class. ู…ุชู‚ุฏุฑุด ุชู‡ุฑุจ ู…ู† ุฏู‡. ูƒู„ method ู„ุงุฒู… ูŠุชู… ุชู†ููŠุฐู‡ุง. ู„ูˆ ู†ูุฐุช ูƒู„ ุงู„ู€ abstract methodsุŒ ุณุงุนุชู‡ุง ุจุณ ุชู‚ุฏุฑ ุชุณุชุฎุฏู… ุงู„ู€ non-abstract method ุจุชุงุนุฉ ุงู„ู€ Parent class.

ุนู…ูˆู…ู‹ุงุŒ ุดูู†ุง ููŠ ุงู„ู€ Inheritance ุฅู† ุงู„ู€ child class ูŠู‚ุฏุฑ ูŠุณุชุฎุฏู… ุงู„ู€ members ุจุชูˆุน ุงู„ู€ parent class ู…ุจุงุดุฑุฉ. ู„ูƒู† ู‡ู†ุงุŒ ุฏู‡ ู…ุด ู…ู…ูƒู†. ู…ุชู‚ุฏุฑุด ุชุณุชุฎุฏู…ู‡ู… ู…ุจุงุดุฑุฉ. ุงู„ุฎุงุตูŠุฉ ุฏูŠ ุชุญุช ู‚ูŠูˆุฏ. ู„ุญุฏ ู…ุง ุงู„ู€ child class ูŠูˆููŠ ุจุงู„ู‚ูŠูˆุฏุŒ ู…ุด ู‡ูŠู‚ุฏุฑ ูŠุณุชุฎุฏู… ุงู„ู€ parent class members.

ู…ุซุงู„ ู…ู† ุงู„ูˆุงู‚ุน: ุงูุชุฑุถ ุฅู† ุฃุจ ูˆุนุฏ ุงุจู†ู‡ ุฅู†ู‡ ู„ูˆ ุฌุงุจ 90% ููŠ ุงู…ุชุญุงู† ุขุฎุฑ ุงู„ุณู†ุฉุŒ ู‡ูŠูƒุงูุฆู‡ ุจู„ุงุจุชูˆุจ. ูุงู„ู„ุงุจุชูˆุจ ู‡ูŠุชุฌุงุจ ู„ู„ุงุจู† ุจุณ ู„ูˆ ุฌุงุจ 90%. ู„ูˆ ุงู„ุงุจู† ุนุงูŠุฒ ูŠุงุฎุฏ ุงู„ู„ุงุจุชูˆุจุŒ ู„ุงุฒู… ูŠูˆููŠ ุจุงู„ู…ุชุทู„ุจุงุช ุงู„ู„ูŠ ุญุทู‡ุง ุฃุจูˆู‡. ุจู…ุฌุฑุฏ ู…ุง ุงู„ุงุจู† ูŠูˆููŠ ุจุงู„ู…ุชุทู„ุจุŒ ุจูŠุงุฎุฏ ุงู„ู„ุงุจุชูˆุจ.

ุฏู‡ ุจุงู„ุธุจุท ู†ูุณ ุญุงู„ุฉ ุงู„ู€ abstract class. ุงู„ู€ abstract class ุจูŠุญุชูˆูŠ ุนู„ู‰ abstract ูˆ non-abstract methods. ู…ู…ูƒู† ุชุนุชุจุฑ ุงู„ู€ abstract method ู‡ูŠ ุงู„ุฏุฑุฌุงุช ุงู„ู„ูŠ ู„ุงุฒู… ูŠุฌูŠุจู‡ุงุŒ ูˆุงู„ู€ non-abstract method ู‡ูŠ ุงู„ู„ุงุจุชูˆุจ. ูู„ูˆ ุนุงูŠุฒ ุชุงุฎุฏ ุงู„ู„ุงุจุชูˆุจ (ุชุณุชุฎุฏู… non-abstract method)ุŒ ู„ุงุฒู… ุชูˆููŠ ุจุงู„ู…ุชุทู„ุจุงุช (ุชู†ูุฐ ูƒู„ ุงู„ู€ abstract methods).

ู…ู„ุงุญุธุฉ: ุนุดุงู† ู†ุนุฑู‘ู method ุฃูˆ class ูƒู€ abstractุŒ ุจู†ุณุชุฎุฏู… ุงู„ู€ abstract keyword.

Example to Understand Abstract Class and Abstract Methods in C#

ุฎู„ูˆู†ุง ู†ูู‡ู… ุงู„ู€ Abstract Class ูˆุงู„ู€ Abstract Methods ููŠ C# ุจู…ุซุงู„. ุจุต ุนู„ู‰ ุงู„ู€ class ุงู„ู„ูŠ ุฌุงูŠ. ุฏู‡ ู‡ูŠูƒูˆู† ุงู„ู€ parent abstract class ุจุชุงุนู†ุง. ููŠ ุงู„ู€ class ุฏู‡ุŒ ุนุฑูู†ุง ุงุชู†ูŠู† non-abstract methodsุŒ ู‡ู…ุง Add ูˆ SubุŒ ูˆุงุชู†ูŠู† abstract methodsุŒ ู‡ู…ุง Mul ูˆ Div.

// The parent abstract class
public abstract class AbsParent
{
    // Non-abstract method with implementation
    public void Add(int x, int y)
    {
        Console.WriteLine($"Addition of {x} and {y} is : {x + y}");
    }
    
    // Non-abstract method with implementation
    public void Sub(int x, int y)
    {
        Console.WriteLine($"Subtraction of {x} and {y} is : {x - y}");
    }
    
    // Abstract method without implementation
    public abstract void Mul(int x, int y);
    
    // Abstract method without implementation
    public abstract void Div(int x, int y);
}

Can we create an instance of an abstract class in C#?

ู„ุฃ. ู…ู†ู‚ุฏุฑุด ู†ุนู…ู„ instance ู…ู† abstract class. ุณูˆุงุก ุงู„ู€ abstract class ุจูŠุญุชูˆูŠ ุนู„ู‰ abstract methods ุฃูˆ ู„ุฃุŒ ุฅู†ุดุงุก instance ู…ู†ู‡ ู…ุณุชุญูŠู„. ู„ูˆ ุญุงูˆู„ุชุŒ ู‡ูŠุฌูŠู„ูƒ compile-time error.

ุงู„ุณุจุจ ู‡ูˆ ุฅู† ู„ูˆ ุงู„ู€ compiler ุณู…ุญ ู„ู†ุง ู†ุนู…ู„ instanceุŒ ูุจุงุณุชุฎุฏุงู… ุงู„ู€ instance ุฏู‡ุŒ ู‡ุชู‚ุฏุฑ ุชู†ุงุฏูŠ ุงู„ู€ abstract methods ุงู„ู„ูŠ ู…ุนู†ุฏู‡ุงุด bodyุŒ ูˆุฏู‡ ู‡ูˆ ุงู„ุณุจุจ ุงู„ู„ูŠ ุจูŠุฎู„ูŠู‡ ู…ูŠุณู…ุญุด ู„ู†ุง ู†ุนู…ู„ instance ู…ู† abstract class.

ุงู„ู€ child class ู‡ูˆ ุงู„ู„ูŠ ูŠู‚ุฏุฑ ูŠุณุชุฎุฏู… ุงู„ู€ members ุฏูŠ. ุงู„ู€ child class ู„ุงุฒู… ูŠุนู…ู„ implement ู„ู„ู€ Mul ูˆุงู„ู€ Div abstract methods ู‚ุจู„ ู…ุง ูŠู‚ุฏุฑ ูŠุณุชุฎุฏู… Add ูˆ Sub. ู„ูˆ ู…ุนู…ู„ุด ูƒุฏู‡ุŒ ู‡ูŠุญุตู„ compile-time error.

Why Abstract Class Cannot Be Instantiated in C#?

ู„ุฅู† ุงู„ู€ abstract methods ุจุชุงุนุชู‡ ู…ุจุชูƒูˆู†ุด ู‚ุงุจู„ุฉ ู„ู„ุชู†ููŠุฐ ู„ุฃู†ู‡ ู…ุด class ู…ูƒุชู…ู„ ุงู„ุชู†ููŠุฐ (fully implemented). ู„ูˆ ุงู„ู€ compiler ุณู…ุญ ู„ู†ุง ู†ุนู…ู„ object ู…ู† abstract classุŒ ู‡ู†ู‚ุฏุฑ ู†ุณุชุฏุนูŠ ุงู„ู€ abstract method ุจุงุณุชุฎุฏุงู… ุงู„ู€ object ุฏู‡ุŒ ูˆุงู„ู€ CLR ู…ุด ู‡ูŠู‚ุฏุฑ ูŠู†ูุฐู‡ุง ูˆู‚ุช ุงู„ุชุดุบูŠู„ (runtime). ุนุดุงู† ูƒุฏู‡ุŒ ุนุดุงู† ูŠู…ู†ุน ุงู†ูƒ ุชู†ุงุฏูŠ ุงู„ู€ abstract methodsุŒ ุงู„ู€ compiler ู…ุจูŠุณู…ุญุด ู„ู†ุง ู†ุนู…ู„ instantiate ู„ู€ abstract class.

ุฏู„ูˆู‚ุชูŠุŒ ุฎู„ูˆู†ุง ู†ู†ูุฐ ุงู„ุงุชู†ูŠู† abstract methods ุฌูˆู‡ ุงู„ู€ child class. ู„ุงุฒู… ู†ุนู…ู„ Implement ู„ู„ู€ abstract methods ุจุงุณุชุฎุฏุงู… ุงู„-override modifier ูƒุงู„ุชุงู„ูŠ:

public class AbsChild : AbsParent
{
    // Providing implementation for the abstract method
    public override void Mul(int x, int y)
    {
        Console.WriteLine($"Multiplication of {x} and {y} is : {x * y}");
    }
 
    // Providing implementation for the abstract method
    public override void Div(int x, int y)
    {
        Console.WriteLine($"Division of {x} and {y} is : {x / y}");
    }
}

ุฏู„ูˆู‚ุชูŠ ู…ููŠุด compile-time error. ุงู„ู€ child class ุฃูˆูู‰ ุจู…ุชุทู„ุจุงุช ุงู„ู€ parent class ุจุชู†ููŠุฐู‡ ู„ู„ู€ abstract methodsุ› ุจุงู„ุชุงู„ูŠุŒ ุงู„ู€ child class ูŠู‚ุฏุฑ ุฏู„ูˆู‚ุชูŠ ูŠุณุชุฎุฏู… ุงู„ู€ non-abstract methods ุจุชุงุนุฉ ุงู„ู€ parent class. ูุชู‚ุฏุฑ ุฏู„ูˆู‚ุชูŠ ุชุนู…ู„ instance ู…ู† ุงู„ู€ Child class ูˆุชุณุชุฎุฏู… ูƒู„ ุงู„ู€ members ูƒุงู„ุชุงู„ูŠ:

using System;
namespace AbstractClassesAndMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            AbsChild absChild = new AbsChild();
            absChild.Add(10, 5);
            absChild.Sub(10, 5);
            absChild.Mul(10, 5);
            absChild.Div(10, 2);
            Console.ReadKey();
        }
    }
   
    public abstract class AbsParent
    {
        public void Add(int x, int y) { /* ... */ }
        public void Sub(int x, int y) { /* ... */ }
        public abstract void Mul(int x, int y);
        public abstract void Div(int x, int y);
    }
    public class AbsChild : AbsParent
    {
        public override void Mul(int x, int y) { /* ... */ }
        public override void Div(int x, int y) { /* ... */ }
    }
}

Output:

Addition of 10 and 5 is : 15
Subtraction of 10 and 5 is : 5
Multiplication of 10 and 5 is : 50
Division of 10 and 2 is : 5

Can we Create a Reference for the Abstract Class in C#?

ุฃูŠูˆู‡ุŒ ู†ู‚ุฏุฑ ู†ุนู…ู„ reference ู„ู„ู€ abstract class ููŠ C#. ู„ูƒู† ู…ู†ู‚ุฏุฑุด ู†ุนู…ู„ instance ู…ู†ู‡. ู‡ู†ุงุŒ ุนู…ู„ู†ุง instance ู…ู† ุงู„ู€ child classุŒ ูˆุจุนุฏูŠู† ุนู…ู„ู†ุง reference ู…ู† ุงู„ู€ abstract class ุจูŠุดุงูˆุฑ ุนู„ู‰ ุงู„ู€ child class instanceุŒ ูˆุจุนุฏูŠู† ุจุงุณุชุฎุฏุงู… ุงู„ู€ reference ุฏู‡ ู†ู‚ุฏุฑ ู†ูˆุตู„ ู„ู„ู€ members.

// Creating abstract class reference pointing to child class object
AbsParent absParent = absChild;
 
// Accessing methods using reference
absParent.Add(10, 5);
absParent.Sub(10, 5);
absParent.Mul(10, 5);
absParent.Div(10, 2);

ู„ุงุฒู… ุชูุชูƒุฑ ุฅู† ุงู„ู€ parent class referencesุŒ ุญุชู‰ ู„ูˆ ุงุชุนู…ู„ุช ุจุงุณุชุฎุฏุงู… child class instancesุŒ ู…ุชู‚ุฏุฑุด ุชู†ุงุฏูŠ ุงู„ู€ child class methods ุงู„ู„ูŠ ู…ุชุนุฑูุฉ ุจุดูƒู„ ูƒุงู…ู„ (pure) ููŠ ุงู„ู€ child class. ุงู„ู€ Overridden methods ู…ุด pure child-class methods. ู„ูˆ method ู…ุนู…ูˆู„ู‡ุง override ููŠ ุงู„ู€ child classุŒ ูู‡ูŠ ูˆุงุฎุฏุฉ ุฅุฐู† ู…ู† ุงู„ู€ parent class. ูุงู„ู€ parent class references ุชู‚ุฏุฑ ุชู†ุงุฏูŠ ุงู„ู€ overridden members ุจุชุงุนุฉ ุงู„ู€ child class ู„ูƒู† ู…ุชู‚ุฏุฑุด ุชู†ุงุฏูŠ ุงู„ู€ pure child class members.

ููŠ ุงู„ู…ุซุงู„ ุฏู‡ุŒ ู„ูˆ ุถูู†ุง method ุงุณู…ู‡ุง Mod ููŠ AbsChild ุจุณุŒ ู…ุด ู‡ู†ู‚ุฏุฑ ู†ูˆุตู„ู‡ุง ู…ู† ุฎู„ุงู„ ุงู„ู€ reference ุงู„ู„ูŠ ู†ูˆุนู‡ AbsParent:

public class AbsChild : AbsParent
{
    // ... Overridden methods
    
    // Pure child class method
    public void Mod(int x, int y)
    {
        Console.WriteLine($"Modulos of {x} and {y} is : {x % y}");
    }
}
 
// In Main method:
// absParent.Mod(100, 35); // This will cause a compile-time error

When to use Abstract Classes and Methods in C#?

ุงู„ู…ูุฑูˆุถ ุชููƒุฑ ุชุณุชุฎุฏู… abstract classes ูˆ methods ููŠ C# ู„ู…ุง ุชูƒูˆู† ุนุงูŠุฒ:

  • ุงู„ู€ Define a Common Base: ู„ู…ุง ุชูƒูˆู† ุจุชุนุฑู‘ู ุฃุณุงุณ ู…ุดุชุฑูƒ ู„ู…ุฌู…ูˆุนุฉ classes ู…ุฑุชุจุทุฉ ุจุจุนุถ.
  • ุงู„ู€ Enforce a Contract: ุงู„ู€ Abstract methods ุจุชุณู…ุญ ู„ูƒ ุชูุฑุถ ุนู‚ุฏ (contract) ู„ุงุฒู… ุงู„ู€ derived classes ุชู„ุชุฒู… ุจูŠู‡.
  • ุงู„ู€ Provide Default Implementations: ุชู‚ุฏุฑ ุชูˆูุฑ implementations ุงูุชุฑุงุถูŠุฉ ููŠ ุงู„ู€ abstract class ูˆุงู„ู€ derived classes ุชุฎุชุงุฑ ุฅู†ู‡ุง ุชุนู…ู„ู‡ุง override ุฃูˆ extend.
  • ุงู„ู€ Implement Polymorphism: ู‡ู…ุง ุฃุณุงุณูŠูŠู† ู„ุชุญู‚ูŠู‚ ุงู„ู€ polymorphism.
  • ุงู„ู€ Create Frameworks and Libraries: ุบุงู„ุจู‹ุง ุจูŠูุณุชุฎุฏู…ูˆุง ู„ุฅู†ุดุงุก frameworks ูˆ libraries ูˆ APIs.
  • ุงู„ู€ Extend Functionality: ุจูŠุณู…ุญูˆุง ู„ูƒ ุชูˆุณุน ุงู„ูˆุธุงุฆู ุจุทุฑูŠู‚ุฉ modular.
  • ุงู„ู€ Ensure Code Consistency: ุจูŠูุฑุถูˆุง ู‡ูŠูƒู„ ู…ุชุณู‚ ููŠ ุงู„ู€ derived classes.
  • ุงู„ู€ Enable Code Reusability: ุจูŠุนุฒุฒูˆุง ุฅุนุงุฏุฉ ุงุณุชุฎุฏุงู… ุงู„ูƒูˆุฏ.
  • ุงู„ู€ Support Extension Points: ุงู„ู€ Abstract methods ุจุชุดุชุบู„ ูƒู€ (extension points).
  • ุงู„ู€ Implement Template Method Pattern: ุจูŠู„ุนุจูˆุง ุฏูˆุฑ ููŠ ุชุทุจูŠู‚ Template Method design pattern.

Summary of Abstract Class and Abstract Methods in C#

  • ุงู„ู€ method ุงู„ู„ูŠ ู…ุนู†ุฏู‡ุงุด body ุจุชุชุณู…ู‰ abstract methodุŒ ูˆุงู„ู€ class ุงู„ู„ูŠ ุจูŠุชู… ุชุนุฑูŠูู‡ ุจุงุณุชุฎุฏุงู… keyword abstract ุจูŠุชุณู…ู‰ abstract class. ู„ูˆ class ุจูŠุญุชูˆูŠ ุนู„ู‰ abstract methodุŒ ู„ุงุฒู… ูŠุชู… ุงู„ุฅุนู„ุงู† ุนู†ู‡ ูƒู€ abstract.
  • ุงู„ู€ abstract class ูŠู‚ุฏุฑ ูŠุญุชูˆูŠ ุนู„ู‰ abstract ูˆ non-abstract methods ู…ุน ุจุนุถ.
  • ุงู„ู€ abstract class ุนู…ุฑู‡ ู…ุง ุจูŠูƒูˆู† ู‚ุงุจู„ ู„ู„ุงุณุชุฎุฏุงู… ููŠ ุญุฏ ุฐุงุชู‡ ู„ุฅู†ู†ุง ู…ู†ู‚ุฏุฑุด ู†ุนู…ู„ object ู…ู†ู‡. ุงู„ู€ members ุจุชุงุนุชู‡ ู…ู…ูƒู† ุจุณ ุงู„ู€ child class ูŠุณุชุฎุฏู…ู‡ุง.
  • ุงู„ู€ Abstract methods ู…ููŠุฏุฉ ู„ู…ุง ุชูƒูˆู† ุจุชู†ุดุฆ base class ุฃูˆ interface ุจูˆุธุงุฆู ู…ุดุชุฑูƒุฉ ุนุงูŠุฒ ุงู„ู€ derived classes ุชูˆุฑุซู‡ุงุŒ ู„ูƒู† ููŠ ู†ูุณ ุงู„ูˆู‚ุช ุชุทู„ุจ ู…ู† ุงู„ู€ classes ุฏูŠ ุฅู†ู‡ุง ุชู†ูุฐ methods ู…ุนูŠู†ุฉ ุจุทุฑูŠู‚ุชู‡ุง ุงู„ุฎุงุตุฉ.