Difference between new and override

后端 未结 14 1778
再見小時候
再見小時候 2020-11-22 05:36

Wondering what the difference is between the following:

Case 1: Base Class

public void DoIt();

Case 1: Inherited class

<         


        
14条回答
  •  天命终不由人
    2020-11-22 06:04

    I had the same question and it's really confusing, you should consider that override and new keywords working only with objects of type base class and value of derived class. In this case only you'll see the effect of override and new: So if you have class A and B, B inherits from A, then you instantiate an object like this:

    A a = new B();
    

    Now on calling methods will take its state into consideration. Override: means that it extends the function of the method, then it uses the method in the derived class, whereas new tell the compiler to hide the method in the derived class and use the method in the base class instead. Here is a very good sight to that subject:

    https://msdn.microsoft.com/EN-US/library/ms173153%28v=VS.140,d=hv.2%29.aspx?f=255&MSPPError=-2147217396

提交回复
热议问题