Home >Backend Development >C++ >What's the Difference Between `new` and `override` in C# Inheritance?
C# Inheritance: Understanding new
and override
In C# inheritance, the keywords new
and override
offer distinct ways to manage method behavior in derived classes. This explanation clarifies their differences and appropriate usage.
new
Modifier: Defining a Separate Method
The new
modifier allows a derived class to introduce a method with the same name as a method in its base class. Crucially, this does not override the base class method; instead, it creates a completely independent method.
Example:
<code class="language-csharp">public class BaseClass { public void MyMethod() { Console.WriteLine("BaseClass Method"); } } public class DerivedClass : BaseClass { public new void MyMethod() { Console.WriteLine("DerivedClass Method"); } }</code>
Here, DerivedClass.MyMethod()
is a distinct method. Calling MyMethod()
on a DerivedClass
instance executes the derived class's version; calling it on a BaseClass
instance executes the base class's version.
override
Modifier: Replacing a Virtual Method
The override
modifier is used to replace the implementation of a virtual method from the base class. The base class method must be declared as virtual
(or abstract
). override
ensures the derived class's version is called when the method is invoked on an instance of the derived class.
Example:
<code class="language-csharp">public class BaseClass { public virtual void MyMethod() { Console.WriteLine("BaseClass Method"); } } public class DerivedClass : BaseClass { public override void MyMethod() { Console.WriteLine("DerivedClass Method"); } }</code>
In this case, DerivedClass.MyMethod()
overrides BaseClass.MyMethod()
. Calling MyMethod()
on a DerivedClass
instance always executes the DerivedClass
version.
Key Differences Summarized:
Feature |
|
|||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Method Creation | Creates a completely new method | Replaces an existing virtual method | ||||||||||||
Inheritance | Breaks the inheritance chain for the method | Maintains the inheritance chain | ||||||||||||
Base Class Method | Base class method remains unaffected | Base class method must be virtual/abstract |
When to Use Each Modifier:
Use new
when you want a completely separate method in the derived class, even if it shares a name with a method in the base class. This is useful for avoiding unintended overriding and maintaining distinct functionality.
Use override
when you intend to extend or modify the behavior of a virtual method defined in the base class, providing a specialized implementation within the derived class while still maintaining the inheritance relationship for that specific method.
By understanding these distinctions, you can effectively utilize inheritance and polymorphism in your C# applications.
The above is the detailed content of What's the Difference Between `new` and `override` in C# Inheritance?. For more information, please follow other related articles on the PHP Chinese website!