Java-polymorphism, Method overloading

Source: Internet
Author: User

What is polymorphic, overloaded 1. Multi-State understanding of polymorphic phenomena

Polymorphism is an important feature of object-oriented. On the concept of definition, I believe there is a lot of information on the Internet, no longer say. Here's my own understanding. Polymorphism means that a method that invokes a class object behaves differently during the run. For the most common example:

Base class: Shape

class Shape{    publicvoiddraw();    publicvoidclear();}

Sub-Category: Circle, Rectangle

class  circle  extends   shape  { public  void  draw ();    //draw a circle  public  void  Clear (); class  rectangle  extends   Shape  { public  void  draw ();    //draw a rectangle  public  void  Clear ();}  

If I declare a Shape reference to a class and then initialize, the last method to call this object draw() :

null;mShape = init();mShape.draw();

Because the object of the class and the object of the Rectangle Circle class can be converted up to Shape an object, when I call the mShape.draw() method, it may draw a circle, or it may draw a rectangle, which depends entirely on init() what the object is returned by the method. This phenomenon is called polymorphism , and this phenomenon is common in reality, for example, cars, electric cars, Cars have 行驶 behavior, for different cars, will take different practical actions to carry out 行驶 This behavior, which is the manifestation of polymorphism in reality.

Timing of polymorphic occurrences

Polymorphism occurs during run time, because we never know which actual object The method returns, because it is not actually running, init() so polymorphism is deferred until run time. However, when the method is static or final decorated, it is bound during compilation. The reason is that the final method does not allow overrides, and the static method is bound to the class, regardless of the object.

2. Overloading

Many people will be overloaded with polymorphic confusion, polymorphic occurrence must have inheritance, override , and overload is not the same, overloading refers to the same method name (refers to the name), there are many different versions, for example, we want to write a add method, if it is two numbers, return the sum of the string. If it's two strings, we'll join the string together. The code is written like this:

class Example{    publicaddInt(intint b){        return"";    }    publicaddString(String a , String b){        new StringBuilder();        temp.append(a);        temp.append(b);        return temp.toString();    }}

But there are many different ways of doing this, and it's easy to confuse. We write several different versions of a method, invoking different versions of the method, depending on the parameters passed in. Therefore, there is a method overload.

class Example{    publicadd(intint b){        return (a + b);    }    publicadd(String a , String b){         new StringBuilder();        temp.append(a);        temp.append(b);        return temp.toString();    }}

Depending on the parameters passed in, the compiler invokes different versions of the method.

The time when the overload occurred

Because the parameters of the method invocation are determined by the compilation period, overloading occurs at compile time .

Precedence of method matching when overloading

(Quoted from http://liujinpan75.iteye.com/blog/495562)

    1. Depending on the method name you call, find out if there is a defined method with the same name, and if not, you will get an error
    2. Compares whether the number of parameters and arguments is equal, and if not, an error occurs. If one or more of the methods meet the criteria, these methods enter the candidate set
    3. Compare the parameter table with the method of the candidate set, if each parameter type in the corresponding position exactly matches, or can be matched by extending the transformation, then the method is called the feasible method and is incorporated into the feasible set. If there is no feasible method, the error will be
    4. Select the best possible method in the feasible concentration according to the following principle, if the best feasible method is 0, it will be an error, otherwise the best feasible method is to finalize the method to be called.

the principle of access is
1. If each parameter can be fully matched, it is the best possible method
2. If each parameter of a method is not worse than the other method, and at least one parameter is better than the other method, it is the best feasible method, here the difference and good is that the exact match is better than the extended conversion, but also the extension of the conversion, there is still good and bad problem, the extension of the transformation has two paths
Byte-short-int-long-float-double
Char-int-long-float-double
The types on the left side of these two paths can be converted to the right type, but the closer the source type is to the target type, the better the conversion.
3. If there are auto-boxing and variable-length parameters, the priority of the extended conversion is greater than auto-boxing, and the priority of automatic boxing is greater than the variable-length parameter.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java-polymorphism, Method 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.