java-Abstract of object-oriented polymorphism

Source: Internet
Author: User

The function of polymorphism is to leave the interface and implementation separated, improve the structure of code, enhance the readability of code, and facilitate the maintenance of code.

In Java, the discussion of polymorphism is the binding of a method invocation, which binds a method call to the same method body . in Java, it is commonly referred to as "runtime binding", also known as "late binding." For example:

1. Define a base class

public class parents{

public void print () {

System.out.println ("parents");

}

}

2. Definition of two derived classes

public class mother extends parents{
@Override
public void print () {
System.out.println ("Mother");
}
Cannot override method of parent class being static or final decorated
This is the equivalent of creating a new tell () method
public static void Tell () {
System.out.println ("Static for Mother");
}
}

Public class Father extends parents {
@Override
public void print () {
System.out.println ("Father");
}
}

3. Test class

public class Test {

public void Find (parents p) {
P.print ();
}
public static void Main (string[] args) {
Test test = new test ();
Mother mother = new mother ();
Test.find (mother);
}

}

The final output is mother, passing the reference of the derived class to the reference of the base class, and then calling the overriding method, where the reference to the base class is able to find the method of the derived class that should be called, because the program is bound at run time.

Key points of note for polymorphism

Except for the static and final methods in 1.java, all other methods are bound at run time. Where private methods are implicitly specified as final, the final method is not bound at run time. When you override a static,final or private method in a base class 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 of the base class.

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

4. The order of object cleanup is the opposite of the Order of creation, of course, if you manually clean up the object because you know the Java garbage collection mechanism.

5. The overridden method in the base class is carefully called in the constructor of the base class, where the object initialization sequence is involved.

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

7. Use the field to express changes in the state by using the difference between the inheritance expression behavior.

How to understand Java polymorphism?

By type conversion, an object is treated as its base class object. Multiple derived classes derived from the same base class can be treated as the same type, and the same processing can be done for these different types. These different derived classes of objects behave differently when they respond to the same method, which is the difference between these similar classes.

Dynamic binding

Connecting a method call and a method body together is called a binding (Bindings).

Depending on the timing of the binding, it is divided into "early binding" and "late binding".

If the program is bound before it runs (with the compiler and linker complete)- early binding ;

If you are binding during a program run, called late binding, late binding is also known as dynamic binding or runtime binding ;

In Java, polymorphism is implemented by dynamic binding, where the Java virtual machine determines which method to invoke with the same name at run time.

Application of polymorphism

Because of polymorphism, a reference variable for a parent class can point to a different subclass object, and the corresponding subclass method is executed at run time based on the actual type of the object to which the parent reference variable points.

Two distribution with polymorphism.

Use polymorphism to design callback methods.

java-Abstract of object-oriented polymorphism

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.