Java learning Materials-method overrides, method overloading, polymorphism and dynamic binding

Source: Internet
Author: User

1. Method coverage

method overrides are subclasses that define a method with the same name as the parent class to overwrite the parent class. When a parent class method is overwritten in some subclasses, it is usually the subclass that calls the parent class and does some extra work.

There are a few things to be aware of when using method overrides:

not using super and wishing to refer to the parent class method leads to infinite recursion, because the subclass method is actually invoking it itself.

When a method is called through a parent class reference, Java correctly chooses the override method for the class that corresponds to that object. For a method provided by the parent class, a subclass can overwrite it, but this is not required, that is, the subclass can also use the parent version of a method.

method Overrides, when a subclass is redefining a method that the parent class already has, it should maintain exactly the same method header declaration as the parent class, the exact same method name as the parent class, the return L type, and the argument list.

Subclasses can add fields, or you can add methods or override methods in the parent class. However, inheritance cannot remove any fields and methods from the parent class.

2. Method overloading

method Overloading is the process of defining several methods with the same name but different parameter lists within their own classes. Unlike the method covered above, method overrides are methods that subclasses override the parent class, and method overloads are methods that are defined within a class of their own, except that the defined methods have the same names but different parameter lists.

Since the same method name appears in a class, it may be asked what the program does when it calls these methods. In fact, when the program calls these methods, the compiler distinguishes them by distinguishing their argument lists. For example, there are now two overloaded methods, some method parameter lists have one argument, and the other has two parameters, and when the program calls this method, the compiler determines which method you want to invoke by finding the number of arguments in the method you want to invoke. So, if you find that the argument list of two or more methods is consistent during overloading, the compiler will make an error, because it cannot tell which method you want.

Any method overloads are allowed in Java. Also note that if the names of the two methods in the overloaded two methods are the same as the argument list, but the method return type is different, this can also compile an error, because this cannot distinguish between overloaded methods.

3, polymorphism is divided into two kinds:

polymorphism is divided into multi-state at compile time and runtime polymorphism.

polymorphic at compile time is implemented by overloading of function overloads or operators. While runtime polymorphism is implemented through inheritance and virtual functions, it is called runtime polymorphism because it is not possible to determine which function should be called according to functions and parameters before execution of the program, but must be dynamically determined in the execution of the program, depending on the execution.

Mechanism for invoking an object method:

The compiler examines the object's claim type and method name.

The compiler then checks the type of the parameter in the method call. If the compiler cannot find any method that matches the parameter type, or if there are multiple methods that match the converted parameter type, the compiler reports an error. Now the compiler knows the name and parameter type of the method that needs to be called.

If the method type is private, static, final, or a constructor, the compiler will also know exactly which method should be called, which is called a static binding. By contrast, the actual type of the implicit argument determines which method is called dynamic binding, which must occur at run time.


Java learning Materials-method overrides, method overloading, polymorphism and dynamic binding

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.