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 Public classA { Publicvirtual String Function () {return"1"; }    }     Public classb:a { Publicoverride string Function () {return"2"; }    }     Public classc:a { Public Newstring Function () {return"3"; }    }     Public classJustfortest { Public voidDotest () {b b=NewB (); C C=NewC ();            Console.WriteLine (B.function ());            Console.WriteLine (C.function ()); Console.WriteLine ((b as A). Function ());//These two are reflected, B or call yourself, return 2Console.WriteLine (c as A). Function ());//C calls the base class, returns 0        }    }
View Code

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 classshape{ Public Static voidMain (string[] args) {Triangle tri=NewTriangle (); System.out.println ("Triangle is a type of shape?" + Tri.isshape ());//InheritanceShape Shape=NewTriangle (); System.out.println ("My shape has" + shape.getsides () + "sides.");// polymorphicRectangle Rec=NewRectangle (); Shape Shape2=Rec; System.out.println ("My shape has" + shape2.getsides (REC) + "sides.");//overloaded   }    Public BooleanIsshape () {return true; }    Public intgetsides () {return0 ; }    Public intGetsides (Triangle Tri) {//overloaded     return3 ; }    Public intGetsides (Rectangle rec) {//overloaded    return4 ; }}classTriangleextendsshape{ Public intGetsides () {//rewrite to implement polymorphic     return3; }}classRectangleextendsshape{ Public intGetsides (inti) {//overloaded    returni; }}
View Code

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.

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.