One name many forms



Download 128,5 Kb.
bet7/7
Sana01.01.2022
Hajmi128,5 Kb.
#294628
1   2   3   4   5   6   7
Bog'liq
Polymorphism in C#

public abstract class AppleBase  

  •    {  

  •        public AppleBase()  

  •        {  

  •        }  

  •   

  •        public abstract void SetName();  

  •    } 



    Derived class MacBook, Overriding a SetName method.

    1. class MacBook : AppleBase  

    2.    {  

    3.        public MacBook()  

    4.        {  

    5.             

    6.        }  

    7.        public override void SetName()  

    8.        {  

    9.            Console.WriteLine("Name: MacBook Pro");  

    10.        }  

    11.    } 



    Let us execute this in the Main method.

    1. class Program  

    2.   {  

    3.       static void Main(string[] args)  

    4.       {  

    5.           MacBook macBook = new MacBook();  

    6.           macBook.SetName();  

    7.       }  

    8.   } 



    Let's see an output.

     

     

    It works, but to take advantage of that constructor in an abstract class, we have to automate something.



     

    It could be initializing properties or anything.

     

    In our case, we can directly call an abstract method SetName() inside a constructor. So that we wouldn't need of calling SetName() method with an object of MacBook in the Main method.



    1. public abstract class AppleBase    

    2.    {    

    3.        public AppleBase()    

    4.        {    

    5.            SetName();    

    6.        }    

    7.     

    8.        public abstract void SetName();    

    9.    }    

    10.     

    11. class Program    

    12.    {    

    13.        static void Main(string[] args)    

    14.        {    

    15.            MacBook macBook = new MacBook();       

    16.           //macBook.SetName();  

    17.        }    

    18.    }   



    And the output will be the same.

     

    Amazing. Finally, mystery solved. Pheww.



     

    Conclusion


     

    In this article, we learned.



    • What is the purpose of the constructor inside an abstract class?

    • What is constructor chaining?

    • How to utilize constructors of abstract classes.

    Download 128,5 Kb.

    Do'stlaringiz bilan baham:
  • 1   2   3   4   5   6   7




    Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
    ma'muriyatiga murojaat qiling

    kiriting | ro'yxatdan o'tish
        Bosh sahifa
    юртда тантана
    Боғда битган
    Бугун юртда
    Эшитганлар жилманглар
    Эшитмадим деманглар
    битган бодомлар
    Yangiariq tumani
    qitish marakazi
    Raqamli texnologiyalar
    ilishida muhokamadan
    tasdiqqa tavsiya
    tavsiya etilgan
    iqtisodiyot kafedrasi
    steiermarkischen landesregierung
    asarlaringizni yuboring
    o'zingizning asarlaringizni
    Iltimos faqat
    faqat o'zingizning
    steierm rkischen
    landesregierung fachabteilung
    rkischen landesregierung
    hamshira loyihasi
    loyihasi mavsum
    faolyatining oqibatlari
    asosiy adabiyotlar
    fakulteti ahborot
    ahborot havfsizligi
    havfsizligi kafedrasi
    fanidan bo’yicha
    fakulteti iqtisodiyot
    boshqaruv fakulteti
    chiqarishda boshqaruv
    ishlab chiqarishda
    iqtisodiyot fakultet
    multiservis tarmoqlari
    fanidan asosiy
    Uzbek fanidan
    mavzulari potok
    asosidagi multiservis
    'aliyyil a'ziym
    billahil 'aliyyil
    illaa billahil
    quvvata illaa
    falah' deganida
    Kompyuter savodxonligi
    bo’yicha mustaqil
    'alal falah'
    Hayya 'alal
    'alas soloh
    Hayya 'alas
    mavsum boyicha


    yuklab olish