Java programming thought eighth chapter polymorphism

Source: Internet
Author: User

Objective:

Encapsulation: Creates a new data type by merging features and behaviors.

Implementation concealment: Separating the interface and implementation by "privatizing" the details.

Polymorphism: Eliminates the coupling between types. Also known as dynamic binding, late binding, or run-time binding.

8.1 Re-discussion upward transformation:

objects can be used either as their own types or as types of base classes. This approach has become an upward transformation.

Its code behaves like this: the parent class refers to a subclass.

Polymorphism allows the use of parameters where the base class is declared at the parameter, and is used to accept base classes or export class implementations.

8.2 Transfer (mainly to describe how to achieve polymorphism)

To solve polymorphic problems, a late-bound method is provided in Java, which is binding at run time based on the type of the object. Also becomes a dynamic binding or run-time binding.

Note: In addition to the static method and the final method in Java, the other methods are late-bound. The final keyword can turn off dynamic binding.

The final keyword is best based on the design to decide whether to use it. It is not possible to improve performance through final because it does not improve performance, and it also makes the design of the program seem confusing.

Polymorphism allows programmers to separate the things that change from the things that are unchanging.

Here again, the private method of trying to rewrite the base class is a very wrong behavior.

Only the invocation of the normal method is polymorphic, and the static method is not polymorphic (in fact final is not, and because private implicitly points to final, so there is no).

8.3 Constructors and Polymorphic

Constructors are not polymorphic, they are essentially static methods, but are implicitly declared.

As mentioned earlier: the constructor of a base class is always called during the construction of the exported class and is called up gradually from the inheritance hierarchy.

For complex inheritance systems:

1) Call the constructor of the base class

2) The initialization method of the member variable is called in the order of Declaration.

3) Call the constructor ontology of the exported class.

Note: In the inheritance system, why is this called? Because the base class is required to exist in the export class, all members of the base class (public protect) can access it.

involves a memory-side construction process:

1) Initialize the storage space allocated to the object into binary 0.

2) Call the base class constructor.

3) invokes the initialization method of the member in the order in which it is declared.

4) Call the constructor body of the exported class.

In fact, in addition to initializing members, the only method that can be safely called in the constructor is the final method of the base class (also applicable to private methods, which are implicitly final).

8.4 Covariant return Type:

This is in jdk1.5.

The overridden method of the exported class can return some type of export of the return type of the base class method.

8.5 Design with Inheritance:

In fact, it shows that when inheriting for design, it must be prudent to:

Inheritance requires that the exact type be known at compile time.

Use inheritance to express differences between behaviors and to express state changes with fields.

Pure inheritance and extension:

Pure inheritance is a is-a relationship: A base class can accept any message that sends an exported class because they have exactly the same interface. We just need to transform from the exported class, never need to know the exact type of the object being processed.

The extends is actually used to extend the interface, which is a is-like-a relationship. The derived class is like a base class, with the exception of the same interface as the base class, and the features that are implemented by the additional methods.

The extended portion of an interface in an exported class cannot be accessed by a base class. For this type, the problem with the upward transformation is that the extension part cannot be accessed by the base class.

In order to solve the upward transformation, the concept of downward transformation is proposed, which can not recognize the extension part.

But there are problems that may arise: you don't know exactly which type to turn into. The runtime exception occurs at this time: (classcastexception).

Virtually every transformation in Java is checked, called "Runtime type Recognition" (RTTI).

  

  

  

  

Java programming thought eighth chapter 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.