Understanding "Constructors" in Inheritance

by kiranpvn kumar on 2/11/2012 6:42:24 PM
Rated 0 from 0 votes
Brief: Demonstrates on explaining "Constructors" when classes are getting inherited
Posted to: Inheritance in .NET Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

If you do not know about Inheritance or constructors in C#, check "Related Posts" section, before going through this article.

If you instantiate a derived class and if there exist any constructors in base class, the constructors in base class get executed first followed by the constructors in derived class.

Using "base" keyword, we can ask a derived class constructor to execute the specific base class constructor (optional) based on the requirements.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConstructorsInInheritance
{
    //Number class has only one number
    class Number
    {
        protected int a;   //Since "a" is declared using "protected" qualifier, it is allowed for access in derived classes  ( but not out side the derived classes )
        public Number() // Parameterized constructor
        {
            a = 0;
        }
        public Number(int p)    // Parameterized constructor
        {
            a = p; 
        }
        public Number(Number n)
        {
            a = n.a;
        }
        public void ShowNumber()
        {
            Console.WriteLine("a = {0}", a);
        }
    }

    //Numbers class is declared with one member but, logically it has Two members due to inheritance
    class Numbers : Number
    {
        private int b; 
        public Numbers() : base() //invokes base class default constructor
        {
            b = 0;
        }
        public Numbers (int a,int b):base(a)    // this will invoke base class constructor (with one parameter) first prior to its own execution
        {
            this.b = b;
        }
        public Numbers(Numbers n) : base (n.a) // this will invoke base class constructor (with one parameter) first prior to its own execution
        {
            b = n.b;
        }
        public void ShowNumbers()
        {
            Console.WriteLine("a = {0}, b = {1}", a, b); // can access both members
        }
 
    }
    
     
    class Program
    {
        public static void Main(string[] args)
        {
            //new object for base class and it contains single member variable
            Number n = new Number(100);
            n.ShowNumber();
 
 
            //new object for derived class with default constructor (no parameter)
            Numbers n1 = new Numbers();
            n1.ShowNumbers();
 
            //another object for derived class to call constructor with two parameters
            Numbers n2 = new Numbers(200, 300);
            n2.ShowNumbers();
 
 
            //another object for derived class to call constructor with object parameter calling parent constructor (called copy constructor)
            Numbers n3 = new Numbers(n2);
            n3.ShowNumbers();
        }
    }
}


Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following


Discussion
No comments yet!