Wednesday, April 22, 2020

How to call base class constructor in derived class

Below is the code with result. Use base keyword to access base class parameterised construtor in derived class


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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
         
            DerivedClass dobj = new DerivedClass();
            DerivedClass dobj1 = new DerivedClass("Pravin");
            Console.ReadLine();
        }
    }
    class BaseClass
    {
       public BaseClass()
        {
            Console.WriteLine("I am base class constructor");
        }
        public BaseClass(string para)
        {
            Console.WriteLine("I am base class constructor with parameter value - " + para);
        }
    }
     class DerivedClass: BaseClass
    {
       public DerivedClass()
        {
            Console.WriteLine("I am derived class constructor");
        }
        public DerivedClass(string para): base(para)
        {
            Console.WriteLine("I am derived class constructor with parameter value - " + para);
        }
    }
}




No comments:

Post a Comment