Home > Backend Development > C++ > Shadowing vs. Overriding in C#: What's the Difference?

Shadowing vs. Overriding in C#: What's the Difference?

DDD
Release: 2025-01-28 14:16:09
Original
654 people have browsed it

Shadowing vs. Overriding in C#: What's the Difference?

Hidden and rewriting in C#: In -depth analysis

In object -oriented programming, the inheritance mechanism is very important in expansion and modification. However, there are two different mechanisms when modifying methods: hiding and rewriting.

Hidden: The method of shielding the foundation

Hidden is a member of the same name as a member who inherited from the base class. Different from rewriting, the derived class will not establish a father -son relationship. Instead, it creates a new entity of hidden foundation members.

The following is an example:

In this scene, the Sayhello () method declared in DOG hides the method inherited from Animal. When the DOG object calls Sayhello (), it will execute the hidden method to block the implementation of the base class.

<code class="language-csharp">class Animal
{
    public virtual string SayHello() { return "Hello from Animal"; }
}

class Dog : Animal
{
    public new string SayHello() { return "Hello from Dog"; }
}</code>
Copy after login
Rewriting: Modify the base class method behavior

On the other hand, rewriting refers to the same signature method of the sub -classes state inheritance as the parent class inheritance. By default, the rewriting method Inherits the access to the modifier and the return type of the base class method. However, they can modify the realization of customs.

In this example, the Sayhello () method in the DOG rewrite the method defined in Animal. When calling on the DOG object, the method of rewriting is provided to provide custom behaviors specific to the dog.

The key difference between hiding and rewriting
<code class="language-csharp">class Animal
{
    public virtual string SayHello() { return "Hello from Animal"; }
}

class Dog : Animal
{
    public override string SayHello() { return "Hello from Dog"; }
}</code>
Copy after login

Access permissions: The access to the base method method of reserving the base class method, and the hidden allowing the derived class to modify it.

Inheritance relationship:
    Rewriting the establishment of a father -son relationship, and hiding members of the base class.
  • Hidden members: The members who may hide inheritance may cause problems when using the base code.
  • Activity Domain: The rewriting method is bound to the inheritance hierarchical structure, and the hidden method is limited to the scope of the derived class.
  • When to use hidden and rewriting
  • Hidden is usually used to avoid naming conflicts in the inheritance level structure, but it can also be used to hide inheritance from the derivative class.
  • On the other hand, rewriting the behavior of the subclass can modify the inheritance method, thereby providing flexibility to define the function without destroying the inheritance tree.

The above is the detailed content of Shadowing vs. Overriding in C#: What's the Difference?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template