Feb 4, 2015

Constructor & Inheritance in C#

Subclass doesn't inherit constructors of its parent class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Constructor
{
    class Program
    {
        class A
        {
            public A ()
            {
                Console.WriteLine("A");
            }
        }
        class B:A
        {
            public B()
            {
                Console.WriteLine("B");
            }
        }
        class C : B
        {
            public C ()
            {
                Console.WriteLine("C");
            }
        }
        static void Main(string[] args)
        {

            A x = new A(); //Result on console: A
            A y = new B(); //Result: AB
            A z = new C(); //Result: ABC

            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment