Methods in Java-overloading

Source: Internet
Author: User

Overloading (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.


The following is an example of overloading:
Package c04.answer;//This is the pack name.
This is the first programming method of this program, first creating an instance of the dog class in the main method, and then invoking the different bark methods with the This keyword in the method of constructing the dog class.

The different overloaded methods bark are differentiated according to their parameter types.

Note: In addition to the constructor, the compiler prohibits invoking the constructor anywhere else.
Package c04.answer;

public class Dog {
Dog ()
{
This.bark ();
}
The Void Bark ()//bark () method is an overloaded method
{
System.out.println (\ "No barking!\");
This.bark (\ "Female\", 3.4);
}
void Bark (String m,double L)//Note: The return value of the overloaded method is the same,
{
System.out.println (\ "A barking dog!\");
This.bark (5, \ "China\");
}
void bark (int a,string N)//cannot differentiate overloaded methods with return values, but only with "parameter type" and "Class name"
{
System.out.println (\ "A Howling dog\");
}

public static void Main (string[] args)
{
Dog dog = new Dog ();
Dog.bark (); [Page]
Dog.bark (\ "male\", \ "yellow\");
Dog.bark (5, \ "China\");

Methods in Java-overloading

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.