Java Programming Ideas: the 8th Chapter polymorphism

Source: Internet
Author: User
Tags export class

In the OOP language, polymorphism is the third basic feature after encapsulation and inheritance.

Encapsulation: Create new data types by merging features and behaviors, and "implementing shadowing" separates interfaces and implementations by "privatizing" the details.

Inheritance: Creates a new type with the extends keyword from an existing type in a multiplexed interface, and allows for an upward transformation.

Polymorphism: Eliminates the coupling relationship between types (what to do and how to do), based on inheritance's upward transformation capabilities, allowing different manifestations of the same type of behavior.

8.1 Further discussion upward transformation

8.1.1 Forgetting Object types

Regardless of the existence of the exported class, the code (method) You write is only for the base class type. You do not need to write your own code for each export type, which is what polymorphism allows.

8.2 Connections

The program is running with a base class type, but how does it know which is the exact type and then invoke the correct method? We need to understand the binding mechanism.

8.2.1 Method Call Binding

Associating a method call with a method body is called binding.

Pre-binding: Before the program executes (implemented by the compiler and the linker), the method call in the C language is pre-bound.

Late binding: Also called dynamic binding, run-time binding, which binds the corresponding method body at run time based on the type of the object.

The default in Java is dynamic binding, without the need for manual setup. Special: The static method and the final (private and final) method are not polymorphic, not dynamic binding.

8.2.2 Produce correct behavior

Dynamic binding allows a base class object in a polymorphic state to correctly execute the corresponding exported class object method.

8.2.3 Scalability

Polymorphism allows the extension of new types and extended base classes to have no effect on existing code (code that calls the base class method). It allows programmers to "leave things that are changed and things that are unchanging."

8.2.4 Defect: Private method cannot be overridden

The private method in the base class can use the same method name and signature in the subclass, but it is a completely new method that does not follow the subclass method we want.

When invoked, the access rights of the base class method are used to determine whether a call can be made.

Whether the subclass overrides the parent class method, and whether the child class can access the parent class to determine if the method can be overridden.

8.2.5 defects: Domain and Static methods

Polymorphic properties (dynamic binding) are only for methods. Domain and static methods do not have this attribute.

Such as: Both the parent class and the subclass have a domain public String str; In super S = new Sub (); S.str out the super, not the sub. However, the general situation does not exist when the domain is set to public and you want to overwrite it with a subclass.

Static methods are also not polymorphic.

8.3 Constructors and Polymorphic

The constructor is an implicit static method and does not have a polymorphic property.

Call order of the 8.3.1 constructor

Why does the compiler enforce that the constructor for each exported class must call the base class constructor: Because the constructor has a special task, check that the object is properly constructed. The export class constructor can only access its own members and cannot access the members of the base class (usually private members). Only the base class constructor has the appropriate knowledge and permissions to initialize its own elements. The initialization of an exported class member may use a base class member, so the exported class is initialized after the base class.

8.3.2 Inheritance and cleanup

When you create a new class by combining and inheriting it, you usually don't need to worry about the cleanup of the object.

But if you do need to clean up, you have to be very careful: after you use it, follow the Create reverse cleanup, sub.dispose () and then Super.dispose () to clean it up.

More complex situation: do not know when to use the end, you need to define the reference count, and then clean up.

Behavior of polymorphic methods inside 8.3.3 Constructors

In the process of calling the subclass constructor, the parent class constructor is called first, when the subclass constructor is not finished, the subclass object is not initialized, and if the Polymorphic method is called in the parent class constructor, the method can produce polymorphic behavior characteristics, but because the subclass constructor is not finished, Therefore, the initialization of the subclass is not complete, and the 0,false,null of the child class member variable in the Polymorphic method can only get the default value.

Object initialization process ( note the distinction between the loading process of a class ):

1. Allocate memory space to the exported class object and initialize to 0,false,null

2. Call the parent class constructor and execute the Polymorphic method to get the domain of the subclass 0,false,null

3. Invoking the initialization of member variables in the order in which they are declared

4. Calling the subclass constructor body

Although the logic here is not a problem, but the behavior is wrong, so when writing the constructor, we want to use a simple way to get the object into normal state, if possible to avoid invoking other methods.

8.4 Covariant return type

Java Programming Ideas: the 8th Chapter polymorphism

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.