All together and Snailren learn java-(vi) Reuse class

Source: Internet
Author: User

 "Lovelorn, alas, has not begun to end ... Alas...... Keep Looking at Java "

Today is the weekend, let's see what the Java reuse mechanism is. As you know, code reuse is very useful, and this feature is an important part of Java. What does Java use to achieve reuse? Is the combination and inheritance. Of course there are proxy mechanisms, but not very common, so we don't consider them for the time being.

What is a "combination"? is to put the instances of different classes into a new class and combine them. That "inheritance" is inherited through the extends keyword. The combination emphasizes the use of a certain kind of function, while inheritance is the customization of a certain kind. So when it comes to inheritance, we try to use a combination when designing. Because the combination is more flexible, it is inherited only when it has to be inherited.

(Small tips: You can add the main method in each class, which makes it easier to debug the unit.) So there are multiple classes in a file that can have more than one main)

For inheritance, the data for the base class is generally defined as private, and the method is defined as public. The initialization of the base class and the object itself is not discussed. At the same time, we know that Java's GC is not controllable, so as previously said, if we have a way to define other memory applications, then we need to do a special cleanup work ourselves. We can use finalize, but we don't recommend this, for the reasons we said before. We can do this with try/finally, because finally is bound to execute, where cleanup work can be called, and the order in which the objects are requested is cleaned up in reverse. (Tip: If you want to overwrite a base class method instead of overloading, to make sure it is overwritten, add @override before the method)

There is also a definition of "upward transformation" that has not been seen before. What is an upward transformation is a subclass that can be used where the method of the base class is called. Because subclasses are one of the base classes.

Finally, let's talk about final. When we use final, we mainly use three aspects, data, methods, classes. When the data is final, the data will not change if it is final for the underlying data type. If the object is final, the object that the reference points to cannot change, but the object itself can be changed, which needs to be noted. To the method with final, meaning is not allowed to overwrite. Previously said that the method with coverage, there is an inline mechanism, can improve efficiency, but now this is not the focus, mainly for the purpose of prohibiting coverage. Final for the class, that is to say that the class cannot be inherited, and that all is final.

All together and Snailren learn java-(vi) Reuse class

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.