Home >Backend Development >C++ >What's the Difference Between `new` and `override` Keywords in Method Inheritance?

What's the Difference Between `new` and `override` Keywords in Method Inheritance?

DDD
DDDOriginal
2025-01-28 11:56:09241browse

What's the Difference Between `new` and `override` Keywords in Method Inheritance?

In -depth understanding of "New" and "Override" keywords

In object -oriented programming, inheritance is the core concept, allowing derivatives to expand and modify the behavior of base classes. When defining these classes, developers may need to rewrite or create new implementation. In order to clarify this difference, let's examine the following scenes:

Scene 1: Base class

This grammar declares a virtual method in the base class. The virtual method can be rewritten in the derivative category to provide specific implementation.

<code class="language-c#">public virtual void DoIt();</code>
Scene 1: Division class

In the derived method, the use of "New" will create a new and independent method, and its name is the same as the method in the base class. However, this is not considered to be rewritten, and when the base class is called, the behavior of the base class method will not affect the behavior of the base class.

Scene 2: Base class
<code class="language-c#">public new void DoIt();</code>

This is a non -futant method and cannot be rewritten in the derived class. The derivatives must use the "NEW" modifier to achieve a new method with the same name. Scene 2: Division class

<code class="language-c#">public void DoIt();</code>

"Override" modifiers indicate that the method in the derivative class is rewriting another method of the same signature in the base class. When calling is called by a base class, the method of rewriting will be executed.

Actual differences

<code class="language-c#">public override void DoIt();</code>

Override:

No matter what the reference type, as long as the method is called, the rewriting method in the derived class will be called.

New: Only when the derived class object is referenced, the new method created in the derived class is called.

  • Recommended usage
  • Usually, when the behavior of extended inheritance methods is recommended, "Override" is recommended. If you want to create a new method with the same name in the derived class, use "New".
  • Example
Consider the following code:

In this example, "derived.doit ()" rewritten "base.doit ()", and when the "B" variable "DOIT ()" method is called from the "B" variable "DERIVED" object, it will be called to call it.

The above is the detailed content of What's the Difference Between `new` and `override` Keywords in Method Inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn