"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.