Sant ku

Sant ku

  • NA
  • 12
  • 1.4k

object vs reference type

Nov 8 2014 9:33 PM

 Consider we have follow scenario

Class base

{
          public void print()
          { 
          Console.writeline("Print from base class");
          } 
 

Class derived : base

{
          public void print()
          { 
          Console.writeline("Print from base class");
          } 
 
and in the main method if we write like this 
main()
{
      base b = new derived();   // My 1st question is what happens at this point in time
      b.print(); 
}                      //Now then why do we need to write like this we could declared it as "base b= new base();"  because anyway we can't call child class variables from b.
//also somebody pointed out that  "base b = new derived();" in this case b become object of child class to a reference variable of base type.
//I want to know what does this mean.  
 
Please suggest with your observations.
 
Thanks in advance 

Answers (1)