Java Learning Notes (eight) polymorphism and abstraction

Source: Internet
Author: User

Polymorphism and abstraction are two important concepts derived from inheritance, polymorphism allows objects to exist in many forms, while abstractions allow the creation of abstract classes.

Polymorphism refers to the ability of an object to have multiple forms. Subclasses of a class can define their unique behavior, while sharing some of the same characteristics of the parent class. Dynamic polymorphism refers to the system in operation to dynamically determine the object that the method refers to.

The technical basis of polymorphism:

Upward Transformation Technique: A reference variable of a parent class can point to a different subclass object, or a child object can be treated as a parent class type;

instanceof Keyword: Used to determine the true type of object at run time;

Dynamic binding technology: The runtime executes the corresponding subclass method according to the actual type of the object referred to by the parent class, thus achieving polymorphism.

Upward transformation: Automatic conversion when converting from low-precision data types to high-precision data types; downward transformation: When converting from a high-precision data type to a low-precision data type, you need to use a forced type conversion. The child class is automatically converted to the parent class, and the parent class must use the coercion type conversion for the child class to achieve a downward transformation, before the conversion is judged by the IF, the keyword instanceof to determine whether the object is an instance of the Post object. ClassCastException type conversion exception.

The invocation of a method is associated with the class in which the method resides, called a binding.

static binding (early bound): The method is already bound before the program executes, and is implemented by the compiler or other linker. For Java, a simple binding that can be understood as a program's compile time, the methods in Java are only final, static, private, and constructors are pre-bound.

Dynamic binding (late binding): Binds at run time based on the type of the specific object. All common methods in Java use dynamic binding techniques.

The main applications of polymorphism:
Polymorphic parameters: That is, when a form parameter of a method is a reference, any object compatible with that reference can be passed to the method, allowing the method to accept formal parameters of different data types. Heterogeneous collections: The most common application of polymorphism is to create a collection of data that is not of the same type, but has a common parent class. A collection of different objects is called a heterogeneous collection.

Abstraction: The keyword abstract, when writing a class, typically defines methods in the class that describe the behavior of the class. In the method body of a class, we write code to implement the behavior that the class performs. Modified classes: Abstract classes cannot be instantiated and require inheritance (no business sense). Modification method: This method has no subject (subject has no practical meaning). Abstract methods must be in an abstract class, and abstract methods must be implemented in subclasses, unless subclasses are also abstract classes. From a design perspective, placing an abstract method in the parent class can force any subclass to implement a particular behavior.

Java Learning Notes (eight) polymorphism and abstraction

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.