Differences between Overload and Override

Source: Internet
Author: User

1. Overloading)

(1) method Overloading is a means for classes to process different types of data in a unified manner. Multiple Functions with the same name exist at the same time and have different parameter numbers/types.

Overload Overloading is a manifestation of polymorphism in a class.

(2) Java method overloading means that multiple methods can be created in the class. They have the same name, but have different parameters and different definitions.

When calling a method, the number and type of different parameters passed to them are used to determine which method to use. This is polymorphism.

(3) During overload, the method name must be the same, but the parameter type and number are different,The return value type can be the same or different. However, the returned value cannot be used as a condition for determining the overload.. (Return types cannot be used as the criteria for distinguishing overload functions)

The following is an example of heavy load:

package com.demo.sw.test;public class OverLoadClass {public void OverLoadMethod(){}public void OverLoadMethod(String s){}public void OverLoadMethod(int n, String s){}public String OverLoadMethod(String s,int n){return null;}}

2. Overriding)

(1) The polymorphism between the parent class and the Child class, and the function of the parent class is redefined.

If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ).

In Java, subclasses can inherit the methods in the parent class without re-writing the same method. But sometimes the subclass does not want to inherit the parent class from the original method, but wants to make some modifications, which requires method rewriting.

(2) If the method in the subclass has the same method name, return type, and parameter table as a method in the parent class, the new method overwrites the original method. If you need the original method of the parent class, you can use the super keyword, which references the parent class of the current class.

(3) The access modification permission of subclass functions cannot be less than that of the parent class;

The following is an example of rewriting:

Package com. demo. sw. test; public class Parent {public void say () {System. out. println ("I am the Parent. ");} public String sayHello () {return" Hello, I am a man. ";} public String sayHello (String name) {return" Hello "+ name + ". ";} public void say (int n) {System. out. println ("what you input in the method is:" + n);}/* public void say (byte B) {System. out. println ("what you input in the method is:" + B);} */private static final String sayH () {// private cannot be changed to public, because the subclass cannot overload this method return "hhh ";}}

package com.demo.sw.test;public class Child extends Parent {public void say(int n){n++;System.out.println("After Override The Result is :" + n);}/*public void say(byte b){b++;System.out.println("After Override The Result is :" + b);}*/public void say(){System.out.println("I am the Child Override My Parent's Method.");}public String sayHello(){return "Hello, I am a boy.";}public String sayHello(String name){return "Hello, " + name + " .";}public static final String sayH(){return "this is the static final method.";}public static final String sayH(String name){return "this is the static final method and you input is :" + name;}}
package com.demo.sw.test;public class TestOverride {public static void main(String[] args) {Parent p = new Parent();//p.say();Parent pc = new Child();pc.say();pc.say(1);pc.say((byte)1);Child chld = new Child();//chld.say();System.out.println(chld.sayH());System.out.println(Child.sayH());System.out.println(Child.sayH("fff"));}}

Concept: Call the object method mechanism.

Insider information about dynamic binding:

1. the compiler checks the object declaration type and method name to obtain all candidate methods. Try to comment out the statement of the Parent class in the above example, and then the compilation will fail.

2. Overload decision-making: the compiler checks the parameter types of method calls and selects the unique one from the preceding candidate methods (implicit type conversion will be involved ). If the compiler finds more than one or not, the compiler reports an error.

Try to comment out the say (byte B) of the Parent class in the previous example. The running result is 2.

3. If the method type is priavte static final and java adopts static compilation, the compiler will know exactly which method to call.

4. When a program is running and dynamic binding is used to call a method, the virtual machine must call the method version of the object that matches the actual type.

In this example, pc actually points to the type of Child, so pc. say (1) calls the subclass say (int n ). However, the subclass does not overwrite say (byte B), so pc. say (byte) 1) calls the say (byte B) of the parent class ).

If you comment out the say (byte B) of the parent class, convert the implicit type to int in step 2, and finally call the say (int I) of the subclass ).

Summary: overloading and rewriting

Overriding and Overloading are different manifestations of Java polymorphism.

Overriding is a manifestation of the polymorphism between the parent class and the Child class, and Overloading is a manifestation of the polymorphism in a class.

If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When the subclass object uses this method, the definition in the subclass is called,

For it, the definition in the parent class is "blocked", and if the method name and parameter type and number of subclasses are the same as those of the parent class, the Return Value Type of the subclass must be the same as that of the parent class;

If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called Overloading ).

The Overloaded method can change the type of the returned value. That is to say, the type of the returned value of the overload can be the same or different.

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.