Labels

Wednesday, June 13, 2007

Overriding & Shadow Myth

Here the efforts is to make understand the myht behind Overiding & Shadowing –

 

class MajorBase

    {

        public virtual string getMessage()

        {

            return "Major Base";

        }

    }

 

class MinorBase : MajorBase

    {

        public string getMessage()

        {

            return "Minor Base";

        }      

    }

 

class Class1 : MinorBase

    {

    }

----------------------------------------------------------------------------------------

Test:-- Without Overriding / With Shadowing – Depends on the Type of Variable

 

class MinorBase : MajorBase

    {

        public new string getMessage()

        {

            return "Minor Base";

        }      

    }

 

MajorBase major = new Class1();

MessageBox.Show(major.getMessage());   à Major

 

MinorBase minor = new Class1();

MessageBox.Show(minor.getMessage());   à Minor

 

MajorBase major = new MajorBase();

MessageBox.Show(major.getMessage());   à Major

 

major = new MinorBase();

MessageBox.Show(major.getMessage());   à Major

----------------------------------------------------------------------------------------

 

Test:-- With Overriding – Depends on the Type of Object

 

class MinorBase : MajorBase

    {

        public overide string getMessage()

        {

            return "Minor Base";

        }      

    }

 

MajorBase major = new Class1();

MessageBox.Show(major.getMessage());   à Minor

 

MinorBase minor = new Class1();

MessageBox.Show(minor.getMessage());   à Minor

 

MajorBase major = new MajorBase();

MessageBox.Show(major.getMessage());   à Major

 

major = new MinorBase();

MessageBox.Show(major.getMessage());   à Minor

----------------------------------------------------------------------------------------

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

No comments:

Post a Comment