More about Virtual / new…plus interfaces!

前端 未结 6 929

Yesterday I posted a question about the new/virtual/override keywords, and i learned a lot from your answers. But still i remain with some doubts.

In between all the

6条回答
  •  醉话见心
    2021-01-01 06:16

    Firstly the explanation of new and virtual

    New

    1. new is used to either new up an object i.e. create an instance of a class or make an object of the class.
    2. new is also used to (override literally) new up the existing method in the base class.

    e.g.

    A a2= new B() (creates a new object using constructor of B, because b is of type A, therefore it works )
    and a2.draw() will result in execution of A class draw because the type is A.
    

    Virtual

    1. This establishes that the method is virtual (i.e. not permanent or physical) and should be overridden, i.e. its definition should be provided int the underlying class.

    override

    1. Keyword used to denote that you would like to override this method etc.

提交回复
热议问题