Java polymorphism features: Reload and overwrite comparisons, and java polymorphism features reload

Source: Internet
Author: User

Java polymorphism features: Reload and overwrite comparisons, and java polymorphism features reload


Java overload:

  • In the same class
  • Methods have the same name, the same or different return values, but multiple methods with different parameters (number or type of parameters)

Public class MethoDemo {public static void main (String args []) {int one = add (); // call the integer addition operation float two = add (10.3f, 13.3f ); // call the addition operation int three = add (10, 20, 30) of the floating point number; // call the addition operation System with three parameters. out. calculation Result of println ("add (int x, int y):" + one); System. out. calculation Result of println ("add (float x, float y):" + two); System. out. println ("add (int x, int y, int z) Calculation Result:" + three);} // defines the method to complete the addition of two numbers, the method returns a public static int add (int x, int y) {int temp = 0; // The parameter in the method is the local variable temp = x + y; // execute the addition calculation return temp; // return the calculation result} public static int add (int x, int y, int z) {int temp = 0; // parameters in the method, is the local variable temp = x + y + z; // execute the addition calculation return temp; // return the calculation result} // defines the method to complete the addition of the two numbers, the Return Value of the method is a float data public static float add (float x, float y) {float temp = 0; // The parameter in the method, which is the local variable temp = x + y; // execute the addition operation return temp; // return the calculation result }};
Output result:

Calculation Result of add (int x, int y): 30

Calculation Result of add (float x, float y): 60

Calculation Result of add (int x, int y, int z): 23.6


Java override:

  • Child class override parent class method, in different classes
  • The override method must have the same method name, parameter list, and return type as the method to be rewritten.
  • You cannot use stricter access permissions than the method to be overwritten.

Class Person {// defines the parent class void print () {// default access permission System. out. println ("Person --> void print (). ") ;}}; Class Student extends Person {// defines the inheritance relationship public void print () {System. out. println (" Student --> void print (). ") ;}}; Public class OverrideDemo {public static void main (String args []) {Student s = new Student (); s. print ();}};
Output result:

Student --> void print ().


Summary: java has three major features: encapsulation, inheritance, and polymorphism. Method overloading and overwriting are the embodiment of polymorphism.




What is the difference between overwriting and overloading in JAVA?

(1) method Overloading is a means for classes to process different types of data in a unified manner. Multiple Functions with the same name exist at the same time and have different parameter numbers/types. Overload Overloading is a manifestation of polymorphism in a class.

(2) 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 calling a method, the number and type of different parameters passed to them are used to determine which method to use. This is polymorphism.

(3) During overload, the method name must be the same, but the parameter type and number are different. The return value type can be the same or different. Return types cannot be used as the criteria for distinguishing heavy-duty functions.

The following is an example of heavy load:
Package c04.answer; // This is the package name
// This is the first programming method of this program. first create a Dog class instance in the main method, and then use the this keyword to call different bark methods in the construction method of the Dog class. Different overload Methods bark are differentiated based on their parameter types.

// Note: Except the constructor, the compiler prohibits you from calling the constructor anywhere else.
Package c04.answer;

Public class Dog {
Dog ()
{
This. bark ();
}
The void bark () // bark () method is the overload method.
{
System. out. println ("no barking! ");
This. bark ("female", 3.4 );
}
Void bark (String m, double l) // Note: The returned values of the overloaded methods are the same,
{
System. out. println ("a barking dog! ");
This. bark (5, "China ");
}
Void bark (int a, String n) // The reload method cannot be distinguished by return values, but can only be distinguished by "parameter type" and "class name"
{
System. out. println ("a howling dog ");
}

Public static void main (String [] args)
{
Dog dog = new Dog ();
// Dog. bark ();
// Dog. bark ("male", "yellow ");
// Dog. bark (5, "China ");

Then let's talk about Overriding)
(1) The polymorphism between the parent class and the Child class, and the function of the parent class is redefined. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). 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 rewriting... the remaining full text>

What is the difference between Polymorphism and overload in JAVA?

Polymorphism refers to a class with multiple functions with the same function name, and multiple parameters.
Overload means that after the subclass inherits from the parent class, it overwrites the original function of the parent class.

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.