I am getting the next classes:
class Base class Der1 : Base class Der2 : Der1
This really is my primary method:
Base b = new Der2() Der1 d1 = new Der2() Der2 d2 = new Der2() b.Print() d1.Print() d2.Print()
The output is
So far as I understand, Override will not let previous approach to run, even when the pointer is pointing for them. Therefore the first line should output
Der2 too. However
Base arrived on the scene.
How is it feasible? The way the override did not work there?
You haven't really overriden the
Base version of
Print(). You've only hidden it having a separate virtual method (named exactly the same) in
If you use the
new keyword on the method signature - you're telling the compiler that this can be a method that unexpectedly happens to have a similar title as an approach to your base classes - but doesn't have other relation. You may make this new method virtual (as is available done) but that is totally different from overriding the bottom class method.
Der2 whenever you override
Der1 - not the version is
Base. Eric Lippert comes with an excellent answer to some slightly different question that will assist you reason about how exactly virtual techniques are treated within the C# language.
Inside your example, whenever you call
Base - therefore the hidden (although not overriden) version of
Der1's implementation, because within this situation, you've really overriden the technique.
Read much more about this within the MSDN documentation of recent and override.
What you might have intended related to Der1 (while you did with Der2) is by using the
class Base class Der1 : Base
It is because
Der1 doesn't override
new keyword). So, once the object is cast to
Base it calls
Base there's no override to call..
override will replace the prior method, but as the
Der1 class does not override
Print() (it Shadows it, to utilize a VB-ism), then your most overriden verion of
Print() is known as, which is the version it defines
As everybody has stated the category
Der1 is changing
Print() rather than overriding it. To determine this for action you can base
Base after which call paper method. It'll then return