Overloads and overrides for methods and methods in Java

Source: Internet
Author: User

Method

  definition: A separate applet defined in a class that has a specific function;

  Benefits of the method:

1, improve the cost of reuse;

2, can simplify the writing;

  Format:

The access modifier modifier Returns a value type method name (parameter type parameter name, ...). ){

function code;

return value;

}

  There are two ways to define a method that need to be clear:

1, the return value type of the method, if the method does not need to have a return value, then the type of the return value is void;

2, need clear parameters;

  Attention:

1, the method to run the return statement as the end. If the method does not return a value, the return statement can be omitted without writing;

2, methods can not be defined in the method;

Overloading of methods

  definition: in the same class, you can define multiple method names consistent, but the parameters of the data type or the number of parameters of different methods, so that multiple methods are called method overloads;

 Attention:

1. The overloads of the method are independent of the return value type of the method;

2, the method's parameter list order is different, also is the method overloading;

override of Method

  Definition: a method in a subclass has exactly the same return value type, method name, number of arguments, and parameter type as the inherited method in the parent class. In this way, the parent class method can be overridden;

  Attention:

1. The parameter list of the parent method must be exactly the same as the parameter list of the method overridden by the quilt class, otherwise it cannot be called overridden but overloaded;

2. The return type of the parent class must be the same as the method return type overridden by the quilt class, otherwise it cannot be called overridden but overloaded;

3, the Java rules, the quilt class overrides the method cannot have more strict access rights than the parent class method;

Summary

  Overloads and overrides of the method:

Overloading-Methods with the same method name and different parameter lists (number or type) are called overloads;

Override-In a subclass, there is a member method with the same method name in the parent class as the parameter list, the same as the return value type, called rewriting;

Note: During the rewrite process, the access limiter for a method in a subclass must be greater than or equal to the access restriction of the method in the parent class;

Private < Default < protected < public

When the parent class refers to a child class object: Father s = new Son ();

The method that is called is the method, depending on the type of object, not the type of reference;

The property that is called is the property of whose, depending on the type of reference, not the type of the object;

In the work, it is not recommended to define a property of the same name in the parent class and subclass;

Overloads and overrides for methods and methods in 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.