Differences between method overloading and rewriting in Java

Source: Internet
Author: User
  Overload and rewriting are two new concepts that make us confused.
The overloading method is in a class with the same method name and different parameters. What is the return type? It can be the same or different.
The overiding method subclass does not want to inherit the parent class method from the original, but wants to make some modifications. This requires method rewriting. Method override is also called method override.

Method Overloading is a means for classes to process different types of data in a unified manner. 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 a method is called, different numbers and types of parameters are passed to them to determine which method to use. This is polymorphism.

Method Rewriting: 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. 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.

Rewrite method rules:
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.
The returned type must always be the same as the return type of the method to be overwritten. Otherwise, it cannot be called a rewrite but a overload.
The access modifier must be greater than the access modifier of the method to be overwritten (Public> protected> default> private)
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. You can throw a non-checked exception.
Reload rules:
Different parameter lists are required;
There can be different return types, as long as the parameter list is different;
Different access modifiers are available;
Different exceptions can be thrown;

Note: Java method overloading requires that methods with the same name have different parameter tables. Only different return types are not enough to distinguish two methods with the same name. The override method can only exist in an inheritance relationship. The override method can only override non-private methods of the parent class.

Here is an example to illustrate method overloading:
Public class testoverload {
Public static void main (string [] ARGs ){
Test test = new test ();
Test. Print (null );
}
}
Class test {
Public void print (string some ){
System. Out. println ("string version print ");
}
Public void print (Object some ){
System. Out. println ("Object version print ");
}
}
The Program The output result is string version print. This topic is obviously a method overload, so that Java classes can have multiple methods with the same method name. The compiler can distinguish them by the type and number of parameters of the method.

Related Article

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.