Analysis of overloading and rewriting in Java

Source: Internet
Author: User
Tags throw exception

Analysis of overloading and rewriting in Java

 Overloading


(1) method overloading is a means of allowing classes to handle different types of data in a uniform manner. Multiple functions with the same name exist at the same time, with different number/types of parameters.

Overloaded overloading is a representation of polymorphism in a class.


(2) Java's method overloading is the ability to create multiple methods in a class that have the same name but have different parameters and different definitions.

The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use.


(3) When overloading, the method name is the same, but the parameter type and number are different, the return value type can be the same or different. The return type cannot be used as a distinguishing criterion for overloaded functions.



 overriding


(1) The polymorphism between the parent class and the child class, redefining the function of the parent 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). In Java, subclasses can inherit methods from the parent class without having to rewrite the same method.

But sometimes subclasses do not want to inherit the parent class's methods, but want to make some changes, which requires a method of rewriting.

Method overrides are also called method overrides.


(2) The method in the Kawai class has the same method name, return type, and parameter table as a method in the parent class, and the new method overwrites the original method.

If you need a method from the parent class, you can use the Super keyword, which references the parent class of the current class.


(3) The access adornment permission of the subclass function can not be less than the parent class;
The following is an example of rewriting:


Concept: The mechanism by which an object method is called.

Insider of dynamic bindings :

1. The compiler checks the type and method name of the object declaration to obtain all candidate methods. Try to comment out the test of the base class above, and then compile it.

2. Overload resolution: The compiler checks the parameter type of the method invocation, and selects the only one from the above candidate (implicit type conversions are in between).

If the compiler finds more than one or does not find it, the compiler will make an error. Try to comment out the test (Byte b) of the previous example base class, and the result is 1 1.

3, if the method type is priavte static final, Java with static compilation, the compiler will know exactly what the call
A method.

4. When a program runs and uses dynamic binding to invoke a method, the virtual machine must invoke a method version that matches the actual type of the object.

In the example, the actual type that B points to is testoverriding, so b.test (0) calls the subclass's test.

However, the subclass does not rewrite test (byte b), so B.test ((byte) 0) calls the parent class's Test (Byte b).

If the parent class (Byte B) is commented out, the second step implicitly converts the type to int, and the final call is the subclass of test (int i).

rules for overriding methods :

1. The parameter list must be exactly the same as the overridden method, otherwise it cannot be called overridden instead of overloaded.

2, the returned type must always be the same as the return type of the overridden method, otherwise it cannot be called overriding but overloaded.

3. The access modifier must be greater than the access modifier (public>protected>default>private) of the overridden method

4. The overriding method must not throw a new check exception or a more general type of check exception than the overridden method declares. For example:

A method of the parent class states that a check exception ioexception, in overriding this method is not able to throw exception, can only throw IOException subclass exception, can throw non-check exception.

Rules for overloading:

1, must have a different list of parameters;

2, can have not scold return type, as long as the parameter list is different;

3, can have different access modifiers;

4, can throw different anomalies;

Analysis of overloading and rewriting 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.