When you do
BaseClass baseObject= new ChildClass();
The static declared type of the object is " BaseClass ".
Therefore, you can only see objects of the "BaseClass" .
If you are sure that the object is of type ChildClass, you can give baseObject a method of "ChildClass" and then use the members of ChildClass
((ChildClass) baseObject). - Intellisense should help provide you with elements of a child class.
Using BaseClass baseObject= new ChildClass(); is the basis for RunTime Politicism . It is very often used if you need to call overridden child logic, but the interface corresponds to the base class
EDIT: An example script in which you used it. The User class got classes called Employee and 3rdPartyUser
The user of the class has a virtual method GetRoleInformation - which is used to obtain information about the role for the user from the Active Directory companies.
However, for a third-party user, since the information does not exist in AD, the logic for obtaining role information involves invoking a web service to retrieve data. In this case, GetRoleInformation is overridden in class 3PartyUser
Now, in the program, on the login page, after authentication, I return an Employee or 3rdPartyUser object. I pass this object to a method with the signature RoleCollection GetRole( User loggedInUser)
Inside this method, without determining the type of user, I simply call loggedInUser.GetRoleInformation and depending on whether it is Employee / 3rdPartyUser , the corresponding base / overridden method will be called and the role data will be obtained from the AD / web service.
In short:
Advantage
BaseClass baseObject= new ChildClass();
ABOVE
ChildClass baseObject= new ChildClass();
is in scripts when you are not sure about the exact type of the child object that will be assigned to the baseObject variable, for example: in this case Employee / 3rdPartyUser For example:
BaseClass baseObject= GetLoggedInUser();
where the signature of this method is User GetLoggedInUser(string userid)
Otherwise, in an example like yours where the object is ALWAYS of type "ChildClass", I believe that there is no advantage to this.