C# Inheritance

Introduction to C# Inheritance

Inheritance एक mechanism होता है जिसके द्वारा दो classes में child/parent relationship establish की जाती है। जब दो classes में child/parent relationship होती है तो child class parent class के members को use कर सकती है।

Inheritance मूलतः inherit word से बना है जिसका अर्थ विरासत में पाना होता है। जब classes के बीच inheritance establish किया जाता है तो inherit करने वाली class (child class) inherit की जाने वाली class (parent class) के members को एक तरह से विरासत के रूप में प्राप्त करती है।

इसके लिए child class में किस प्रकार के declaration आदि की आवश्यकता नहीं होती है। Inheritance के द्वारा child class parent class के members को इस प्रकार access करती है जैसे वे उसी class में define किये गए है।

उदाहरण के लिए जब आप कोई नयी class create करें तो यह जरुरी नहीं है की आप नए filelds और methods define करें। यदि ऐसी कोई class है जिसके members को use किया जा सकता है तो आप उसे inherit कर सकते है और उसके members को use कर सकते है।

Inheritance mechanism की advantages निचे दी जा रही है।

  • Reusability - Inheritance के द्वारा आप class के code को दूसरी class में use कर पाते है। इससे आपके code की reusability बढ़ती है।
  • Readability - Inheritance establish करने से extra code लिखने की आवश्यकता नहीं होती है जिससे आपके program में code कम हो जाता है जो readability को बढ़ाता है।
  • Save Programmers Time - Inheritance establish करने से programmer का time बचता है क्योंकि एक ही code को बार बार लिखना नहीं पड़ता है।

Inherit की जाने वाली class को parent या base class कहा जाता है। Inherit करने वाली class child या derived class कहलाती है।

C# में सिर्फ single inheritance ही allow है। एक class सिर्फ एक ही class को inherit कर सकती है। कोई भी class एक से अधिक classes को inherit नहीं कर सकती है। लेकिन एक class एक से अधिक interfaces को implement कर सकती है।

Single inheritance के अलावा C# में multilevel inheritance भी allow है। यानी inherit करने वाली class भी किसी दूसरी class द्वारा inherit की जा सकती है।

एक class दूसरी class के constructors और finalizers को inherit नहीं कर सकती है। इसके अलावा एक class दूसरी class के private members को भी नहीं inherit कर सकती है।

लेकिन यदि inherit करने वाली class inherit की जाने वाली class में nested है तो वह private members को access कर सकती है। Private members को छोड़ कर बाकी सभी members child class में accessible होते है।

Syntax of C# Inheritance

C# में किसी class को inherit करने का general syntax निचे दिया जा रहा है।

class parent-class
{
    //parent class members
}

class child-class:parent-class
{
   //child class members
  //use parent class members also
}

जैसा की आप ऊपर दिए गए synax में देख सकते है जब किसी class को inherit किया जाता है तो inherit करने वाली class को declare करते समय उसके नाम के बाद colon (:) लगाकर उस class का नाम लिखा जाता है जिसे inherit किया जाएगा।

Example of C# Inheritance

C# में inheritance के use को निचे उदाहरण द्वारा समझाया जा रहा है।

using System;

class myClass
{
    public void display()
    {
         Console.WriteLine("This is parent class display method");
    }
}

class myOtherClass:myClass
{

}

class myFinalClass
{
    static void Main(String[] args)
    {
         myOtherClass obj = new myOtherClass();
         obj.display();
    }
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

This is base class display method

The base Keyword

यदि parent और child class में एक ही नाम के members create किये गए है तो child class से base class के members को access करने के लिए base keyword की आवश्यकता होती है।

उदाहरण के लिए ऊपर दिए गए उदाहरण में यदि myOtherClass में भी एक display() method create किया जाए और इस class का object create करके display() method को call किया जाय तो myOtherClass का ही display() method execute होगा।

ऐसे में myClass के display() method को call करने के लिए आपको base keyword का उपयोग करना होगा। इसे निचे उदाहरण द्वारा समझाया जा रहा है।

using System;

class myClass
{
    public void display()
    {
        Console.WriteLine("This is parent class display method");
    }
}

class myOtherClass:myClass
{
     public void display()
    {
         base.display();
         Console.WriteLine("This is child class display method");
    }
}

class myFinalClass
{
    static void Main(string[] args)
    {
        myOtherClass obj = new myOtherClass();
        obj.display();
    }
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

This is parent class display method
This is child class display method

Overriding Methods

कई बार आपको parent class के methods को child class के अनुरूप change करने की आवश्यकता हो सकती है ताकि उन्हें child class में use किया जा सके।

Parent class के methods को child class में change किया जा सकता है। इसे overriding कहा जाता है। क्योंकि आप parent class के methods की definition child class में override करते है।

आप parent class के जिस method को child class में override करना चाहते है उसे parent class में virtual keyword के साथ define करते है। इसका syntax निचे दिया जा रहा है।

public virtual return-type member-name
{
    //Code here...
}

Child class में method को override करते समय उन्हें override keyword के साथ define किया जाता है। इसका syntax निचे दिया जा रहा है।

public override return-type member-name
{
    //Code here...
}

C# में methods को override करना निचे उदाहरण द्वारा समझाया जा रहा है।

using System;

class colorClass
{
    public virtual void display()
    {
        Console.WriteLine("This is color class");
    }
}

class redColorClass:colorClass
{
    public override void display()
    {
        Console.WriteLine("This is red color class");
    }
}

class finalColorClass
{
   static void Main(string[] args)
   {
     colorClass obj = new colorClass();
     obj.display();

     obj = new redColorClass();
     obj.display();
   }
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

This is color class
This is red color class