Object-oriented override (override) differs from overloading (overload)

Source: Internet
Author: User

First, rewrite (override)

Override is a way to override (overwrite) a method to implement different functions. It is generally used to override (re-implement) a method in the parent class when the subclass inherits the parent class.

Override (Overwrite) the rule:

1. The parameter list of the overridden method must be exactly the same as the overridden method , otherwise it cannot be called overridden instead of overloaded .

2. The access modifier for the overridden method must be greater than the access modifier (public>protected>default>private) of the overridden method.

3. The return value of the overridden method must be consistent with the return of the overridden method;

4. The overridden method throws an exception that must be the same as the exception thrown by the overridden method, or its subclass;

5. The overridden method cannot be private, otherwise only a new method is defined in its subclass, and no s has overridden it.

6. Static methods cannot be overridden as non-static methods (compile error).


override is a subclass that implements the method of the parent class again.            New means that this method is a subclass of its own, with no inheritance relationship to the parent class, just the same name as public class A {public virtual string Function () {        return "1";        }} public class B:a {public override string Function () {return ' 2 ';        }} public class C:a {public new string Function () {return ' 3 ';            }} public class Justfortest {public void dotest () {b b = new B ();            c C = new C ();            Console.WriteLine (B.function ());            Console.WriteLine (C.function ()); Console.WriteLine ((b as A). Function ());//The two reflect, B or call itself, return 2 Console.WriteLine (c as A). Function ());//c calls the base class, returns 0}} 
Second,overload are overloads, which are generally used to implement several overloads within a class, with the same names and different parameter forms.

rules for overloading :

1, in the use of overloading can only be achieved by the same method name, different parameter form. Different parameter types can be different parameter types, different parameter numbers, different parameter order (parameter types must not be the same);

2, can not be overloaded by access rights, return type, thrown exception;

3, the method of the exception type and number will not affect the overload;

The concept of polymorphism is more complex and has multiple meanings, and an interesting but not rigorous argument is that inheritance is a method of using the parent class of a subclass, and polymorphism is the method by which the parent class uses subclasses.

In general, we use polymorphism in order to avoid heavy overloading in the parent class causing the code to be bloated and difficult to maintain.

As an example:

public class shape{public   static void Main (string[] args) {     Triangle tri = new Triangle ();     System.out.println ("Triangle is a type of shape?" + Tri.isshape ());//Inherit     shape shape = new Triangle ();     System.out.println ("My shape has" + shape.getsides () + "sides."); Polymorphic     Rectangle Rec = new Rectangle ();     Shape shape2 = Rec;     System.out.println ("My shape has" + shape2.getsides (REC) + "sides."); Overload   } public   Boolean Isshape () {     return true;   }   public int getsides () {     return 0;   }   public int getsides (Triangle tri) {//overload     return 3;   }   public int getsides (Rectangle rec) {//reload    return 4;   }} Class Triangle extends shape{public   int Getsides () {///override to implement Polymorphic     return 3;}   } Class Rectangle extends shape{public   int getsides (int i) {//overload    return i;   }}

Note that the method of the Triangle class is overridden, whereas the method of the Rectangle class is overloaded. Comparing the two, you can find the advantages of polymorphism to overloading:

In the case of overloading, each subclass in the parent class is overloaded with a method that obtains the number of edges;

If polymorphism is used, the parent class provides only the interface that obtains the number of edges, and the number of sides to which the shape is obtained, which is implemented ( rewritten )in the subclass.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Object-oriented override (override) differs from overloading (overload)

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.