Java method overloading, rewriting, super, this, final keyword

Source: Internet
Author: User
I. Heavy Load

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. Return types cannot be used as the criteria for distinguishing heavy-duty functions.

Ii. Rewrite

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. Method override is also called method override. (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;


Rules for override and reload methods:

Rewrite method rules: 1. the parameter list must be exactly the same as that of the method to be overwritten. Otherwise, it cannot be called a rewrite but a overload. 2. The return type must always be the same as the return type of the method to be overwritten. Otherwise, it cannot be called an overwrite but an overload. 3. The access modifier must be greater than the access modifier of the method to be overwritten (Public> protected> default> private) 4. The rewrite method cannot throw a new check exception or a broader check exception than the declared method. For example, a method of the parent class declares an ioexception check. If you override this method, you cannot throw an exception. You can only throw a subclass exception of ioexception, but can throw a non-check exception. Reload rules: 1. Different parameter lists must be available; 2. Return types that are not scolded, as long as the parameter list is different; 3. Different access modifiers are available. 4. Different exceptions can be thrown. The difference between rewriting and overloading is that rewriting polymorphism works, calling methods that have been overloaded can greatly reduce the amount of input to the code. The same method name can have different functions or return values as long as different parameters are passed to it. A clear and concise class can be designed with good rewriting and overloading. It can be said that rewriting and overloading play an extraordinary role in coding.

Iii. Final

According to the context of the program, the Java keyword final has the meaning of "This cannot be changed" or "final state". It can modify non-abstract classes, non-abstract class member methods, and variables. You may need to block changes in two ways: design or efficiency. The final class cannot be inherited and has no subclass. The methods in the final class are final by default. The final method cannot be overwritten by the quilt class, but can be inherited. Final member variables indicate constants and can only be assigned once. The values do not change after the values are assigned. Final cannot be used to modify the constructor. Note: The Private member method of the parent class cannot be overwritten by the quilt class method. Therefore, the private type method is of the final type by default.
4: This

Describe under what circumstances to use this:

First, call another constructor using this (parameter list). This is only used in the constructor of the class and cannot be used in other places. 2. When the name of a function parameter or local variable in the function is the same as that of a member variable, the member variable is blocked. To access the member variable, you must use "this. to reference member variables. Of course, without the same name, you can directly use the name of the member variable instead of this. If you use this name, it is no error. Third, when the function needs to reference the current object of the class to which the function belongs, use this directly.
V. Super

The key of super is similar to this. It is a blocked member variable or member method or becomes visible, or used to reference the blocked member variable and member method. However, super is used in subclass to access the blocked members in the direct parent class. Note that it is a direct parent class (that is, the nearest superclass above the class ). 1. To call the constructor of the parent class in the subclass constructor, use the "super (parameter list)" method to call the constructor. The parameter is not required. Note that the "super (parameter list)" statement can only be used in the first row of the subclass construction method body. 2. When the local variable in the subclass method or the member variable of the subclass has the same name as the parent member variable, that is, when the subclass local variable overwrites the parent member variable, use "super. the member variable name to reference the parent class member variable. Of course, if the parent class member variables are not overwritten, you can also use "Super. member variable name" to reference the parent class member variables, but this is unnecessary. Third, when the member methods of the subclass overwrite (override) The member methods of the parent class, that is, the subclass and the parent class have the same method definition (but the method body can be different ), in this case, use "super. method (Parameter List) to access the parent class.


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.