Java Programming Idea (Second Edition) 7th chapter: Polymorphism (polymorphism)

Source: Internet
Author: User
Tags abstract constructor contains execution final functions garbage collection inheritance
Programming encapsulation, inheritance and polymorphism are the three core essences of OOP.
Method-call (function Call) binding method:
If the binding action occurs before the program executes (completed by the compiler and the connector), it becomes the pre-binding (early binding)
The binding action is performed on an object-type Biell during the execution period, called a late-bound late binding, or the execution-time binding run-time binding, or dynamically binding dynamic binding.
Java uses RTTI (run-time type Identification) technology to support dynamic binding.

The final keyword modifier mentioned in the previous chapter has two effects: 1 is designed to prohibit the method from being overwritten, and 2 is to tell the compiler to "close" the dynamic binding in terms of efficiency, so the compiler is able to take the final method Call produces more efficient program code. But most of the time it does not bring overall performance improvements to your program. So it's best to be in the design to decide whether to use final, rather than try to use final to improve efficiency.

Override and overload are only slightly different in grammar, but semantically 108000. Be careful not to be negligent in your mistakes. If this problem is buried in large programs, it can be imagined how difficult it is to find this problem!

Abstract keyword: to produce a common interface or to prevent the generation of instances of base class, you can use an abstraction class.
Classes that contain abstract methods functions are called abstract class. If class contains single or multiple abstract methods, You need to use the keyword abstract as the modifier for this class. Otherwise the compiler will issue an error message.

We can also declare the class without any abstract methods as abstract. If you do not want the class you are writing to be generated by any entity, but this class does not have the actual reason to "have abstract methods", Such a nature would be extremely useful.

Constructors (constructors) and polymorphism (polymorphism)
A constructor on the entire inheritance chain is invoked when an object is generated, otherwise the entire object cannot be constructed successfully.

Inheritance and Finalize ()
When you use combinatorial techniques to write new classes, you don't need to worry about the termination (finalizing) of his member objects. Each member is a stand-alone object and is therefore reclaimed and terminated by the garbage collector. But in an inheritance relationship, if you have some special cleanup action (cleanup) You have to do it in garbage collection, you have to make a copy of Finalize in derived class, and don't forget to call Finalize () of base class. That is, super.finalize ().

The behavior of Plymorphic Mehtods in constructors
--It's hard to predict: think about it. This is a bug-producing area. As far as the concept is concerned, the task of the constructor is to make the object from scratch (which is difficult to be considered a general work). Therefore, when composing a constructor, one principle is: "Get the object into the correct state as simply as possible." If you can, Do not call any functions. The only function in the constructor that can be called safely is the final function in base class (this is true for private functions as they are born final). Such functions cannot be overwritten, and there is no ambiguity.

Pure inheritance (pure inheritance) vs. expansion (extension):
Is-a vs. Is-like-a

Downward transition (downcasting) and Rtti
Rtti contains more connotations than mere transformations. For example: There is a way to get the type you are dealing with before you try to go down.

As seems to be mentioned in the previous chapter: member functions are not allowed to call constructors, and we are now trying to limit the invocation of member functions ....


--------------------------------------------------------------------------------



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.