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 Base, Der2, Der2.

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 Der1.

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.

In Der2 whenever you override Print you're really overriding the 'new' version that you simply declared in 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 Print, you're calling it within the first situation via a reference of type Base - therefore the hidden (although not overriden) version of Print is known as. Another two calls are sent to 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 override keyword:

class Base





class Der1 : Base





It is because Der1 doesn't override Print, it replaces it having a completely new method that unexpectedly happens to have a similar title (this really is triggered through the new keyword). So, once the object is cast to Base it calls Print in 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 Base's 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 d1 and d2 to Base after which call paper method. It'll then return Base.

((Base)d2).Print() //Base