Monday, 26 November 2012

what is Constructor overloading ?


Yes, till now we know that method overloading. Now we will see constructor overloading.

Points Keep in Mind :
Execution of derived classes starts from base class constructor.******maindatory
Based on signature of the constructor their apporiate parent constructor will be executed
(other wise parent default constructorà parameter less Ex: People() executes).

As we want to create a parameterized constructor we must create parameterless constructor also.


class People
    {
        public string Name{get;set;}
        public int Age {get;set;}
        public People ()
       {
            this.Name=Name;
            this.Age=Age;
               }
       public People (string name)
               {
            this.Name=name;
               }
       public People(int age)
       {
           this.Age = age;
       }
       public People (int age,string name)
               {
           this.Age = age;
           this.Name=name;         
               }
       public People(string name,int age)
       {          
           this.Name = name;
           this.Age = age;
       }        
    }
    class Developer : People
    {
        public Developer():base("kasi",41)   //  base --> people provides 4 signatures.
        {                                                     // People(string name,int age)
            Console.WriteLine("Developer name is :"+Name);
            Console.WriteLine("Developer Age is: " + Age);
        }     
   public Developer(int age)           
 {
          // executes People() constructor as default.        
            Console.WriteLine("Developer is :" + age);
 }             
    }
    class Designer : People
    {
          public Designer(): base(42, "Ravi")  //  People (int age,string name)
        {
            Console.WriteLine("Designer name is:" + Name);
            Console.WriteLine("Designer age is:" + Age);
        }
        public Designer(int age):base(42)  
              // executes People(int age) constructor here.
        {
            Console.WriteLine("Designer age is :" + Age);
        }   
    }

No comments:

Post a Comment