Understanding JAVA Polymorphism __java

Source: Internet
Author: User
These days I am reviewing the basics of Java language, although with the team, the individual has carried out the actual project development, but the more go to the above feel that they should take a moment to look at the basic knowledge of Java, consolidate the foundation. Today, the review is polymorphic, at the same time write down their own learning experience.

Data abstraction, inheritance and polymorphism are three characteristics of object-oriented programming language. Polymorphism, I think its role is to be used to separate the interface and implementation, improve the structure of the code, enhance the readability of the code. In some very simple cases, we may not use polymorphism to develop programs that meet our needs, but in most cases, without polymorphism, the code is extremely difficult to maintain.

In Java, talking about polymorphism is discussing the binding of method calls, which is associating a method call to the same method body. In C, the binding of a method (called a function in C) is implemented by the compiler, which in English is called the early binding (early binding), so it is natural to think of the corresponding late binding (late binding), which is often called run-time in Java Binding (Run-time binding), I personally feel that this is more appropriate, run-time binding is the purpose of the code when the runtime can determine the type of object. A simple example illustrates:

/**

* Define a base class

*/

Public Class Parents {

public void print () {

System.out.println ("parents");

}

}

/**

* Define two derived classes

*/

Public Class Father extends Parents {

public void print () {

System.out.println ("father");

}

}

Public Class Mother extends Parents {

public void print () {

System.out.println ("Mother");

}

}

/**

* class to test output results

*/

Public Class Test {

public void Find (Parents p) {

P.print ();

}

public static void Main (string[] args) {

Test T = new Test ();

Father f = new Father ();

Mother m = new Mother ();

T.find (f);

T.find (m);

}

}

The final output is father and mother, the reference of the derived class to a reference to the base class, and then the override method is invoked, and the reference to the base class is able to find the method that should call that derived class, because the program is bound at run time.

People who have learned the basics of Java can easily understand the code and polymorphism, but there are some key points to be aware of, a summary of their own polymorphism:

1. In addition to the static and final methods in Java, all other methods are run-time bound. In my other article, when the private method is implicitly specified as final, the final method is not bound at runtime. When you override the static, final, or private method in a derived class, you are essentially creating a new method.

2. In a derived class, it is best to use a different name for the private method in the base class.

3. Classes that contain abstract methods are called abstract classes. Note that the definition contains such meaning, as long as the class contains an abstract method, the class is an abstract class. Abstract classes are derived as the role of the base class, providing a common interface for different subclasses.

4. The order in which objects are cleaned is in contrast to the order in which they were created, assuming, of course, that they want to manually clean the object, as everyone knows the Java garbage collector.

5. In the constructor method of the base class, the overridden method in the base class is called carefully, which involves the order of object initialization.

6. The construction method is implicitly declared as a static method.

7. Use the difference between the behavior of inheritance expression, use the field to express the change of state.

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.