Summary differences between rewriting and overloading

Source: Internet
Author: User

The rewrite rules are as follows:


1. Parameter List: it must exactly match the parameter list of the method to be overwritten.
2. Return type: It must be exactly the same as the return type or child type declared in the override method in the superclass.
3. access level: it must not be stronger than the method to be overwritten, but weaker than the method to be overwritten.
4. Non-check exception: the rewrite method can throw any non-check exception, regardless of whether the overridden method declares the exception.
5. Check exception: the rewrite method must not throw a new check exception, or a broader check exception than the check exception declared by the override method
6. The final and static methods cannot be rewritten.

Rules of the overload method:
1. parameter list: the list of parameters must be changed for the overloaded method.
2. Return type: the return type can be changed.
3. modifier: modifier can be changed
4. Exception: new or more extensive exceptions can be declared.

 

  1. ClassAnimal {
  2. Public VoidEat (){
  3. }
  4. }

The following lists various override and heavy load instances of the animal eat method. Based on the Rules listed above, determine whether the method is legal (the following methods belong to the animal subclass horse method ):
1. Private void eat () {} cannot be compiled, illegally rewritten, And the access modifier becomes restrictive. It is not overloaded because the parameter list has not changed.
2. Public void eat () Throw runtimeexception {} can be rewritten through compilation. The rewrite method can throw any non-checking exception.
3. public void eat () Throw ioexception {} cannot be compiled or overwritten illegally. The rewrite method must not throw a new check exception or a wider check exception than the check exception declared by the override method; the parameter list must be changed before it is overloaded.
4. Public void eat (string food) {} can be compiled, not overwritten; Legal overload, because the parameter list has changed.
5. Public String eat () {} cannot be compiled, not overwritten, because its return type is not overloaded, because the parameter list has not changed.
6. Public String eat (int n) {} can be compiled, not overwritten; it is a heavy load. First, the parameter list must change, and the return type can change.

 

Make a change to the method whose parent class is overwritten:

 

  1. ClassAnimal {
  2. PublicAnimal eat ()ThrowsIoexception {
  3. Return Null;
  4. }
  5. }

 

7. Public animal eat () {return NULL;} can be compiled and legally rewritten without throwing an exception, as long as no new or more extensive exception is thrown.
8. Public animal eat () throws filenotfoundexception {return NULL;} can be compiled to overwrite non-checked exceptions that belong to subclasses.
9. Public animal eat () throws exception {} cannot be compiled or illegally rewritten, throwing a wider range of exceptions.
10. Public horse eat () {} can be rewritten by compiling, because the return type can be a subclass of the overwritten return type.

 

 

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 at the same time
Yes, with 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,
Different parameters and definitions are available. The number and type of different parameters passed to a method
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.
It can also be different. Return types cannot be used as the criteria for distinguishing heavy-duty functions.

 

2 overriding)
(1) The polymorphism between the parent class and the Child class, and the function of the parent class is redefined. If you define

The method has the same name and parameter as its parent class. We say this method is overwritten ). In Java,
Subclass can inherit the methods in the parent class without re-writing the same method. But sometimes subclass does not want to be original
Instead, we want to modify the method of the parent class. This requires method rewriting. Method Rewriting
Method coverage.
(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.
It 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;

 

Summary: Reload and rewrite (overwrite ).

Overriding and overloading are different manifestations of Java polymorphism. Rewrite
Overriding is a manifestation of the polymorphism between the parent class and the Child class, and overload Overloading is a class polymorphism.
. If you define a method in a subclass with the same name and parameters as its parent class, we will say that this method
Overriding ). When the subclass object uses this method, it will call the definition in the subclass and
The definition in the parent class is "blocked", and if the method name and parameter type and number of child classes are both
If the parent class is the same, the return value type of the subclass must be the same as that of the parent class.
Method, which has different numbers of parameters or different parameter types, is called method overload.
(Overloading ). The overloaded method can change the type of the returned value. That is to say, the returned results of the overload
The value types 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.