Can someone explain me the difference between instantiating a derived class by referring through the base class and instantiating a derived class by the derived class name itself.
Please go through this program
using System;
abstract class Test
{
public int _a;
public abstract void A();
}
class Example1 : Test
{
public override void A()
{
Console.WriteLine("Example1.A");
base._a++;
}
}
class Example2 : Test
{
public override void A()
{
Console.WriteLine("Example2.A");
base._a--;
}
}
class Program
{
static void Main()
{
// Reference Example1 through Test type.
Test test1 = new Example1();
test1.A();
Example2 test2 = new Example2();
test2.A();
}
}
Here is the question again, inside the Main() method two objects are created.
First object test1 is referenced through Test(base class) type, what does it actually mean ? and what difference does it make comapred to the second object which is based on the derived class ?
In a program which is preferred, referencing the base class or the derived class, how does it help ?
I know my question is a little vague but any idea on this would be very helpful for me to learn further. Thanks in advance.