Thursday, August 8, 2019

Constructor callin in inheritance in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{//Base class
    class Base
    {
        public Base(){ Console.WriteLine("Constructor: Base"); }
    }
    //Derived class
    class DerivedOne : Base
    {
        public DerivedOne(){ Console.WriteLine("Constructor: DerivedOne"); }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Base o = new DerivedOne();
            Console.ReadKey();
        }
    }
}

Output:
Constructor: Base
Constructor: DerivedOne

No comments:

Post a Comment