The difference between overload and override C + + Java

Source: Internet
Author: User

Overload: As the name implies, it is over (re)--load (load), so Chinese names are overloaded.

It can express the polymorphism of the class, can be the function inside can have the same function name but the parameter name, return value, type can not be the same;

Alternatively, you can change the parameter, type, return value, but the function name remains the same.

Override: Is the meaning of Ride (rewrite), when the subclass inherits the parent class, you can define a method with the same name and argument as its parent class.

When a subclass calls this function, the subclass's method is called automatically, and the parent class is overwritten (overridden).

The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism.

Overriding overriding is a representation of polymorphism between a parent class and a subclass,

Overloaded overloading is a representation of polymorphism in a class.

If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding).

When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked".

If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types.

is called a method's overload (overloading). The overloaded method is to change the type of the return value.

The difference between overload and override Override (override)
1, method name, parameter, return value is the same.
2. The subclass method cannot reduce the access rights of the parent class method.
3. The subclass method cannot throw more exceptions than the parent class method (but the subclass method can not throw an exception).
4, exists between the parent class and the child class.
5. The method is defined as final and cannot be overridden.
Overload (Heavy Duty)
1, the parameter type, number, order at least one is not the same.
2. You cannot overload a method name that has only a different return value.
3, exists in the parent class and subclass, in the same class.
inheritance in Java, Method overrides (overrides) the difference between override and overloaded overload of a method
The rewriting of methods (overriding) and overloads (overloading) are different manifestations of Java polymorphism.
Overrides (overriding) are a representation of the polymorphism between a parent class and a subclass, whereas overloading (overloading) is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). when an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is masked. if more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types or have different parameter orders .is called a method's overload (overloading). cannot be overloaded by Access permission, return type, thrown exception.

1. Override Features
1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;
2. The return value of the overridden method must be the same as the return of the overridden method;
3. The exception that is thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
4. The method is defined as final and cannot be overridden.
5, for inheritance, if a method in the parent class is the access permission is private, then it can not be overridden in the subclass overrides,if defined, it simply defines a new method without overriding the override effect. (It usually exists between the parent class and the child class.) )

2.Overload Features
1. You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different order of parameters(Of course, several parameter types within the same method must be different, such as fun (int, float), but not fun (int, int));
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;
4. Overloaded events typically occur in the same class, between different methods.
5, exist in the same class, but only virtual methods and abstract methods can be overwritten.

Its specific implementation mechanism:

Overload is overloaded, overloading is a parameter polymorphism mechanism, which is a polymorphic mechanism implemented by different types or numbers of parameters. is a static binding mechanism (at compile time you already know which code snippet is executing).

Override is overwrite. Overlay is a polymorphic mechanism of dynamic binding. That is, an element of the same name, such as a member function, has different implementation codes in the parent class and child classes. What code is executed depends on the actual runtime. Overrride instances:
classa{ Public intGetval () {return(5); }   }   classBextendsa{ Public intGetval () {return(10); }   }    Public classOverride { Public Static voidMain (string[] args) {b b=NewB (); A A= (A) b;//cast B to the type of a     intx=A.getval ();   SYSTEM.OUT.PRINTLN (x); }   } 

Results: 10

Overload instances:

//Demostrate method Voerloading. classOverloaddemo {voidTest () {System.out.println ("no parameters"); }   voidTestinta) {System.out.println ("a:"+a); }//end of overload test for one integer parameter. voidTestintAintb) {System.out.println ("a and b:"+a+" "+b); }DoubleTestDoublea) {System.out.println ("Doublea:"+a); returnA *A; }}   Public classoverload{ Public Static voidMain (string[] args) {Overloaddemo ob=NewOverloaddemo (); Doubleresult;      Ob.test (); Ob.test (10); Ob.test (10, 20); Result= Ob.test (123.25); System.out.println (" Result of Ob.test (123.25): "+result);}}

Results:

NO parameters
A:10
A and B:10 20
Double a:123.25
Result of Ob.test (123.25): Differences between overload, overwrite, and override in 15190.5625 C + +

Overload (Heavy Duty ):

In a C + + program, several functions that are semantically and functionally similar can be represented by the same name.

However, the parameters or return values are different (including type, order), which is function overloading.

(1) the same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) Thevirtual keyword is optional.

Override (Overwrite ): Refers to a derived class function that overrides a base class function, characterized by:

(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword.

Overwrite (override ): Refers to a function of a derived class that masks a base class function with the same name as the following rule:

(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different.
At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).


(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword.
At this point, the function of the base class is hidden (be careful not to confuse the overlay).

#include <stdio.h>#include<iostream>classparent{ Public:    voidF () {printf ("parent.f ()/n"); }    Virtual voidG () {printf ("parent.g ()/n"); }    intADD (intXinty) {returnX +y; }    //overloaded (overload) Add function    floatADD (floatXfloaty) {returnX +y; }};classchildone:parent{//overriding (overwrite) parent class functions    voidF () {printf ("childone.f ()/n"); }    //Overwrite (override) The parent virtual function, mainly to implement Polymorphic    voidG () {printf ("childone.g ()/n"); }};intMain () {Childone childone;//= new Childone ();parent* p = (parent*) &Childone; //call PARENT.F ()P->F (); //Implementing PolymorphicP->G (); Parent* P2 =NewParent (); //overloading (overload)printf"%d/n", P2->add (1,2)); printf ("%f/n", P2->add (3.4f,4.5f)); DeleteP2; System ("PAUSE"); return 0;}

The difference between overload and override C + + Java

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.