One name many forms



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

public class AppleBase  

  •    {  

  •        public AppleBase()  

  •        {



  •            Console.WriteLine("1. Base class: AppleBase");  

  •        }  

  •    } 



    The derived class: MacBook. It is also printing a message in its constructor.

    1. class MacBook : AppleBase  

    2.    {  

    3.        public MacBook()  

    4.        {  

    5.            Console.WriteLine("2. Derived class: MacBook");  

    6.        }  

    7.    } 



    Now if I create an object of MacBook. what do you think what will happen?

    1. class Program  

    2.    {  

    3.        static void Main(string[] args)  

    4.        {  

    5.            MacBook macBook = new MacBook();  

    6.        }  

    7.    } 



    Let's see what will happen.

     

     

    Analyze the output, even if we create an object of a MacBook, Compiler still calls an AppleBase class's constructor prior to MacBook's.



     

    This concept is known as constructor chaining. 

     

    What did this tell us?



     

    Exactly my thoughts. which means we do have another way to call a constructor. 

     

    Object creation does not hold a monopoly over constructor executions, Right?



     

    So what do we know?



    • We know that Abstract classes can have constructors.

    • And we can not instantiate an abstract class.

    What did we learn?

    • We can call the Abstract class's constructor through constructor chaining.

     Let's have our abstract class back and let's have an abstract method to define the name of the product.


    1. 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