4
Reply

NP11 B MyB = new D()

Maha

Maha

Jun 12 2007 6:46 PM
3k

June 12, 2007

  

Hi Guys

 

In the below program I can agree that “D MyD = new D();” here the type D and constructor D both are same, as usual. But I couldn’t understand why “B MyB = new D();” here type B and constructor D are not same, that means letter B and letter D are not same. This is unusual.

 

Please anybody explain why both are different. Website address is given.

 

Thank you

 

//http://www.java2s.com/Code/CSharp/Language-Basics/Inheritance3.htm

 

using System;

 

namespace Client.Chapter_5___Building_Your_Own_Classes

{

      public class InheritanceChapter_5___Building_Your_Own_Classes

      {

            static void Main(string[] args)

            {

                  B MyB = new D();

                  D MyD = new D();

 

                  //Both result in in D's instance of Display being //called

                  MyB.Display();

                  MyD.Display();

            }

      }

      public class B

      {

            public virtual void Display()

            {

                  Console.WriteLine("Class B's Display Method");

            }

      }

      public class C: B

      {

            public override void Display()

            {

                  Console.WriteLine("Class C's Display Method");

            }

      }

      public class ContainedClass

      {

            int MyInt = 0;

      }

      public class D: C

      {

            public ContainedClass MyClass = new ContainedClass();

            public override void Display()

            {

                  Console.WriteLine("Class D's Display Method");

            }

      }

 

}

/*

Class D's Display Method

Class D's Display Method

Press any key to continue

*/


Answers (4)