Java method overloading and polymorphism

Source: Internet
Author: User
Tags modifier qtest

Let's take a look at what is method overloading?

The requirements for method overloading are: The method name is the same, the parameter list is different (different parameter types or parameter order or number of parameters). The other parts of the method, such as the method return value type and modifier, have no relation to the method overload. It is best to add @overload annotations.

Why do you use method overloading?

The purpose of overloading is to make it easier for programmers to invoke methods. For example, System.out.println () This function is used to output, when the output is an integer is used in this method, when the output of a string is still used this method. The matching method is automatically found based on the different parameters passed in.

The following program does not use method overloading, try to analyze to see what shortcomings.

PublicClassmethodtest07{
PublicStaticvoidMainString[] (args) {
Cons: For programmers, there are three different ways to remember
System.Out.println (Compute.sumint (128,42));
System.Out.println (Compute.sumdouble (121.23,242.432));
System.Out.println (Compute.sumlong (234L,3242L));
}
}
Classcompute{
PublicStaticIntSumint (IntAIntb) {
ReturnA+b;
}
Public static double sumdouble (double A,double b) {
return a+b;
}
Public Static long sumlong (long A,long b) {
return a+b;
}
}

The flaw in the code above is that programmers have to remember three different methods. (Head Large)

Combine the code to see which cases belong to the method overload? The code is followed by a detailed comment.

The following methods form the method overloads
PublicStaticvoidM1(IntA) {}
PublicStaticvoidM1(DoubleA) {}

PublicStaticvoidM2(IntAIntb) {}
PublicStaticvoidM2(IntA) {}

PublicStaticvoidM3(IntADoubleb) {}
PublicStaticvoidM3(DoubleAIntb) {}

The following M4 methods do not form method overloads
PublicStaticvoidM4(IntA) {}
PublicStaticvoidM4(Intb) {}Method repeats, compilation cannot pass

The M5 method does not form an overload of the method overloading method and is independent of the return value type of the method
PublicStatic  void  M5 () {}
public  static   int  M5 () {
  return  ;
 }
//M6 does not constitute overloads of the method   methods are overloaded with the method's modifier list independent of
static   void  M6 () {}
public  static  void  M6 () {}
 

To summarize, the overloads of the method:

1. Occurs in the same class

2. Method names are the same

3. The parameter list is different (type, number of parameters, different order)

4. Independent of return value type

5. Regardless of the modifier list of the method

What is polymorphic?

Polymorphism means that objects of different classes are allowed to respond to the same message. That is, the same message can take many different behaviors depending on the sending object (sending the message is the function call). Inheritance is being prepared for polymorphic implementations. Subclasses inherit the parent class, we can write a reference to the parent class type of the subclass, which can handle either the parent class object or the subclass object, and when the same message is sent to the child class or the parent class object, the object performs different behavior depending on the reference to which it belongs, which is polymorphic. That is, polymorphism is the same message that makes different classes respond differently.

There are three prerequisites for implementing polymorphism in Java: inheritance, rewriting, and upward transformation.

Inheritance: Subclasses and parent classes that have inheritance relationships must exist in polymorphism.

Rewrite: Subclasses redefine some methods in the parent class and call the methods of the subclasses when they are called.

Upward transformation: In polymorphic, a reference to a subclass needs to be assigned to the parent class object, so that the reference can have the ability to invoke methods and subclasses of the parent class.

Only by satisfying the above three conditions can we use the same logic implementation code in the same inheritance structure to handle different objects, thus achieving different behavior. The following is a combination of code understanding.

PublicClassQuadrangle{
Instantiate an array object that holds a quadrilateral object
Privatequadrangle[] Qtest =Newquadrangle[6];
PrivateIntNextindex =0;
PublicvoidDraw(Quadrangle Q){Define the Draw () method with the parameter as a Quadrilateral object
If(Nextindex < Qtest.length) {
Qtest[nextindex] = q;
System.out.println (Nextindex);
nextindex++;
}
PublicStaticvoidMain(string[] args){
Instantiate two quadrilateral objects for calling the draw () method
Quadrangle q =NewQuadrangle ();
Q.draw (NewSquare ());Call the Draw () method with a square object as a parameter
Call the Draw () method with the parallelogram object as a parameter
Q.draw (NewParallelogramgle ());
}
}
ClassSquareExtendsQuadrangle {&NBSP; //define a square class, inherit the Quad class
  public&NBSP; Square ()  {
   system.out.println (" Square ");
 }
}

//define a parallelogram class, inherit the Quad class
class  parallelogramgle&NBSP; extends &NBSP; quadrangle {
  public&NBSP; parallelogramgle ()  {
   system.out.println (" parallelogram ");
 }
}

Output:

正方形
0
平行四边形
1

As you can see from the results of this example, calling the draw () method with different class objects for parameters can handle different image class problems, and programmers do not have to define methods that perform the same function in all subclasses, as long as they instantiate a subclass object that inherits the parent class to invoke the method, as long as the method in the parent class is maintained.

Search the public number "programmer Koala", welcome attention!

Java method overloading and polymorphism

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.