Difference between override and overload in C #

Source: Internet
Author: User

Overload: In the same scope, the same parameter method with the same name, but the return value cannot constitute a overload.

Override: During the inheritance process, the Child class overrides the parent class method, and the Parent and Child class methods are the same.The base class method is declared as virtual (virtual), and used in the derived classOverrideDeclare that this method is overwritten. 

Hide: do not declare the base class method (non-virtual method by default ).NewHide this method. 

During overload, select the method to be called Based on the parameter;
When rewriting, accessing the parent subclass calls the override method of the subclass;

When hidden, accessing the parent class calls the method of the parent class and the method of the subclass class.

 Hidden example:

1 Using System;
2 Class A
3 {
4 Public   Void F ()
5 {
6 Console. writeline ( " A.f " );
7 }
8 }
9 Class B:
10 {
11 New   Public   Void F ()
12 {
13 Console. writeline ( " B. f " );
14 }
15 }
16 Class Test
17 {
18 Static   Void Main ( String [] ARGs)
19 {
20 B = New B ();
21 B. F ();
22 A = B;
23 A. F ();
24 }

25 }

The output is
B. f

A.f 

 Virtual method rewriting example

  Using System;
Class A
{
Public   Virtual   Void F ()
{
Console. writeline ( " A.f " );
}
}
Class B:
{
Public Override Void F ()
{
Console. writeline ( " B. f " );
}
}
Class Test
{
Static Void Main ()
{
B = New B ();
B. F ();
A = B;
A. F ();
}

  The output is B. f
B. f

 Supplement: override is generally used for method rewriting of interface implementation and inheritance classes. Note that

1. the logo of the covered method must match the logo of the covered method to achieve the coverage effect;
2. The returned value of the overwritten method must be the same as that of the overwritten method;
3. The exception thrown by the overwriting method must be the same as that thrown by the overwriting method, or its subclass;
4. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.