“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

Hello everyone, I'm GW_gw. I'm glad to learn and progress with you.Copy the code

The following content is from the network, if there is any infringement, please contact me to delete, this article is only used for learning exchange, not for any commercial purposes.

Abstract

This article focuses on method Overload and Override in Java.Copy the code

Method overloading and overwriting

1. Method overload

Method overloading: Methods have the same name and different argument lists, regardless of the return value type.

Parameter lists differ in the following situations:

  • The number of parameters is the same and the type is different.
  • The number of parameters is different, and the type is the same or different.
  • Parameters of multiple types are in different order.

Examples show:

Public class OverloadTest {public static void main(String[] args) {system.out.println (sum(1,2)); System. The out. Println (sum (2, 3)); } public static int sum(int a,int b){ return a + b; } public static int sum(int a,int b,int c){ return a + b +c; }}Copy the code

Method overloading makes it easy to use the same method name for many different, similar functions.

When using the same method name, the JVM selects overloaded methods based on the parameters passed in, and it is critical that the JVM does not create ambiguities when selecting methods.

2. Method rewrite

Method override (overwrite, overwrite) :

  1. The method in the subclass has the same name and argument list as the method in the parent class,
  2. The return value range of subclass methods is smaller than the return value range of superclass methods. (For example, if the parent method returns object and the subclass method returns int, this is fine, but the reverse is an error.)
  3. The permission modifier of a subclass method must be greater than or equal to the permission modifier of its parent class. (For example, if the parent is public, the subclass must be public, and if the parent is protected, the subclass must be protected or public.)

If all of these requirements are met, then we say that the subclass method overrides the parent class method.

Method rewrite features:

If you create a subclass object, the subclass method is preferred. (Often encountered in downward transitions of polymorphic objects)

Examples show:

Public class Father {public void speak(){system.out.println (" I am a parent!" ); }} public class extends Father{@override public void speak(){system.out.println (); ); } } import OverrideDemo.Child; import OverrideDemo.Father; public class OverrideTest { public static void main(String[] args) { Father child = new Child(); child.speak(); }}Copy the code

It is recommended to write @override before an overridden method to help check if it is really an overridden method and avoid errors.

conclusion

These are the features and introductions of overloading and overwriting, and I hope you don’t confuse silly friends. If there is a wrong place, welcome to dig friends comment correct.