I’ve got 5 years experience. But to be honest while I know and use this construct…
Baseclass bc = new DerivedClass();
I have no idea what it actually does, not really, not truly. Is it a derived class or a base class? And I know that if I call bc.Method() I will get the derived class method. But only if I use the new keyword or override or something. To be honest I start to get a bit fuzzy here, I think I need to go back to basics with this, can anybody point me in the right direction?
You are creating an instance of DerivedClass and assigning it to a variable of type BaseClass. This can be useful if you have something like a base class Animal with two derived classes Fish and Dog, but you do not know which one will be instantiated and this is not important in your context because you are going to invoke a method which is defined in the base class. For instance you could have something like this:
Be careful with the use of modifiers, because if you define Feed in Animal with virtual and redefine it in Fish with override, the Fish version will be executed (it is linked at execution time). If you dont’t mark the base version as virtual and mark the Fish version as new, the Animal version is executed (it is linked at compile time).