I. Basic definitions

The constructor

Constructors are a special kind of method. It is used primarily to initialize an object when it is created, that is, to assign initial values to its member variables. It is always used with the new operator in the statement that creates the object. In particular, a class can have more than one constructor, which can be distinguished by the number or type of its arguments. That is, constructor overloading.

Overloading (Overloading)

Method overloading is when multiple methods in the same class have the same name, but these methods have different argument lists, and the number of arguments or types of arguments cannot be exactly the same.

I fell in love with strawberry ice cream when I saw it in an ice cream shop on the street. After many years, in the same place, met the same ice cream, but at this time, can not produce the love in those days. The same street (class), the same ice cream (method), because of the condition change (parameter type), can no longer produce love (return result), at this point, my feelings are overloaded (maybe hate, maybe indifferent).

Overriding.

Method overrides exist between subclasses, and subclasses define methods with the same method name, argument list, and return type as those in the parent class.

Note: 1. Subclasses cannot override final methods in their parent class. 2. Subclasses must override abstract methods in their parent class

\


Two, implementation method

41. Overriding:

In general, derived classes inherit methods from base classes. Therefore, when an object inheritance method is called, the implementation of the base class is called and executed. However, sometimes you need different implementations of inheritance methods in derived classes. For example, if an animal class has a “run” method, deriving horses and dogs from it, horses and dogs have different forms of running, so the same method requires two different implementations, which would require “rewriting” the methods in the base class. Overriding a base-class method means modifying its implementation or rewriting it in a derived class.

Overloading (Overloading) :

Create more than one procedure, instance constructor, or attribute in a class with the same name but different parameter types.


3. Detailed understanding

The scope of The method name The list of parameters The modifier Throws an exception that does not exist in the parent class The return type And object oriented
overloading The same class The same different Has nothing to do can different polymorphism
rewrite Different class The same The same Greater than a superclass method Can not be The same inheritance

Now we know that we can Overriding.

  1. Polymorphism between parent and subclass redefines the function of the parent class. If we define a method in a subclass that has the same name and parameters as its parent, we say that method is Overriding. But sometimes subclasses don’t want to know how to Overriding their parent’s methods, but rather that they want to modify them, and that requires Overriding methods.
  2. If a method in a subclass has the same method name, return type, and argument list as a method in the parent class, the new method overrides the old method. If you want a method existing in the parent class, you can use the super keyword, which references the parent class of the current class.
  3. A subclass function cannot have less access modifier than its parent.
Why override a parent class method
Typically, subclasses inherit the methods of their parent class, and when object inheritance methods are called, the implementation of the parent class is called and executed. However, sometimes you need to implement inheritance methods in subclasses differently. For example, suppose an animal has a way of "running", from which two subclasses of dog and horse are inheritedCopy the code
How to rewrite
Override A method that overrides A parent class using the override keyword. To override a method of a parent class, the parent class must declare the method to be overridden as virtual or Abstract. Adding the virtual keyword to a method to be overridden in a parent class indicates that its implementation can be overridden in a subclass. C. tual the virtual keyword is used to define methods that support polymorphism. Methods with the virtual keyword are called "virtual methods".Copy the code
[Access modifier] Virtual [return type] method name (argument list) {// Implementation of a virtual method that can be overridden by subclasses}Copy the code

Rewrite examples:

Namespace inheritDemo2 {class Employee {public virtual void EmpInfo() {console. WriteLine(); } } class DervEmployee : Employee { public override void EmpInfo() { base.EmpInfo(); // The base keyword will be mentioned in the extension below console. WriteLine(" this method overwrites the base method "); } } class Test { static void Main(string[] args) { DervEmployee objDervEmployee = new DervEmployee(); objDervEmployee.EmpInfo(); / / note: Instances of objDervEmployee derived classes are references to objEmployee assigned to the Employee class, Employee objEmployee = objDervEmployee; objEmployee.EmpInfo(); }}}Copy the code

Extension: The base keyword is used to access superclass members from subclasses. Even if a method of a parent class is overridden in a subclass, it can still be called using the base keyword. Furthermore, you can use the base keyword to call the constructor of the parent class when creating a subclass instance. Use the base keyword to access only the constructors, instance methods, or instance properties of the parent class, not the static methods of the base class.

\

Overloading (Overloading)

  1. Method overloading is a way for classes to handle different types of data in a uniform way. Multiple functions with the same name exist at the same time with different parameter numbers and types. Overloading is a manifestation of polymorphism in a class.
  2. When overloaded, the method name is the same, but the parameter type and number are different, and the return value type can be the same or different. Overloaded functions cannot be distinguished by return categories.

Fourth, summarize the rules

(1) Overridden methods must have the same argument list as overridden methods, otherwise they cannot be overridden but overridden. (2) Overridden methods must have more access modifiers than overridden methods (public>protected>default>private). 3. The return value of the overridden method must be the same or compatible with the return value of the overridden method. The overridden method must throw the same exception as the overridden method, or a subclass of it. The overridden method cannot be private, otherwise the subclass defines a new method and does not override it. 6. Static methods cannot be overridden as non-static methods (compiler error); 7. When a parent method is final, a subclass cannot override the method, regardless of whether the method is public, protected, or default.Copy the code
(2) the rules of overloading: 1, when using overloading can only be realized by the same method name, different parameter form. Different parameter types can be different parameter types, different parameter numbers, and different parameter sequences (parameter types must be different). 2. Access permissions, return types, and thrown exceptions cannot be overloaded; 3. The type and number of exceptions in a method do not affect overloading.Copy the code

Five, there are Easter eggs at the end of the article

Interview question: The difference between Overload and Override. Can overloaded methods be differentiated by return type? Answer: Method overloading and overwriting are both ways of implementing polymorphism, the difference being that the former implements compile-time polymorphism, while the latter implements runtime polymorphism. Overloading occurs when a method with the same name has different argument lists (different parameter types, different number of arguments, or both) and is considered overloaded. Rewriting occurs between a subclass and its parent class. Rewriting requires that the subclass overridden method have the same argument list as the parent overridden method, have compatible return types, be more accessible than the parent overridden method, and declare no more exceptions than the parent overridden method (The Richter substitution principle). Overloading has no special requirements for return types and cannot be distinguished by return types.