The only difference between the two is that if you do
DerivedClass classinst = new DerivedClass();
The compiler interprets classinst
as a DerivedClass
, and allows you to use all the specific methods/properties/etc you have declared in the derived class and not in the base class.
On the other hand, if you do
BaseClass classinst = new DerivedClass();
The compiler will interpret classinst
as a BaseClass
, and you will not have access to those members.
Do realize that the actual type of the object does not change. The reference to the object stays the same, no matter which of the two declarations you use. The only thing that changes is how the reference is interpreted, and therefore what members are available; those of the base class, or the derived class.