Java class regeneration

Source: Internet
Author: User

"One notable feature of Java is the reuse or regeneration of code." But what's most revolutionary is that there are other things we can do much more than copy and modify the code. ”

In programmed languages like C, the reuse of code is already feasible, but the effect is not particularly significant. As elsewhere in Java, the solution is a class-related problem. We reuse code by creating new classes, but we don't have to recreate them, and we can directly use ready-made classes that others have built and debugged.

But this must be done without interfering with the original code. In this chapter, we will introduce two ways to achieve this goal. The first is the simplest: simply create an object of the original class in the new class. We call this method "compositing" because the new class is merged by an object of an existing class. We simply reuse the functionality of the code rather than take it in its form.

The second approach appears to be somewhat tricky. It creates a new class as a "type" of an existing class. We can take the form of an existing class as it is and add new code to it without having an impact on existing classes. This magical behavior is called "Inheritance" (inheritance), and most of the work involved is done by the compiler. For object-oriented programming, "Inheritance" is one of the most important basic concepts. It will have some extra impact on what we're going to be talking about in the next chapter.

For both synthesis and inheritance, most syntax and behavior are similar (since they all generate new types based on existing types). In this chapter, we'll delve into the mechanics of these code regeneration or reuse.

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.