Java method overloading and method Rewriting

Source: Internet
Author: User

 

Method overload (Overloading method)

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 name 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. The following is an example of method overloading:

Class methodoverloading {
Viod receive (int I ){
System. Out. println ("received one int data ");
System. Out. println ("I =" + I );
}
Viod receive (float f ){
System. Out. println ("received one float data ");
System. Out. println ("F =" + F );
}
Viod receive (string s ){
System. Out. println ("received a string ");
System. Out. println ("s =" + S );
}
}

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.

overiding method
in Java, subclasses can inherit the methods in the parent class, you do not need to rewrite 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 rewriting is also called method overwrite. If the method in the subclass has the same method name and return type and parameter table , the new method overwrites the original method.
if you need the original methods of the parent class, you can use the super keyword, which references the parent class of the current 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.