Reusing code is one of the many compelling features of Java.
In general, implementing code reuse Java provides two ways: composition and inheritance.
- Composition: The new class consists of an object of an existing class. (Reuse the functionality of existing code, not its form)
- Inheritance: Builds a new class according to the type of the existing class. (To reuse the form of an existing class and add new code to it, without changing the existing class).
Combination
classengine{ Public voidstart () {} Public voidstop () {}}classdoor{ Public voidopen () {} Public voidclose () {}}classwindow{ Public voidrollup () {} Public voidRolldown () {}} Public classCar {PrivateEngine engine=NewEngine (); PrivateDoor door=NewDoor (); PrivateWindow window=NewWindow (); Public Static voidMain (string[] args) {Car car=NewCar (); Car.engine.start (); Car.door.close (); Car.window.rolldown (); } }
You can see that the combination is explicitly placing child objects in the new class, whereas inheritance is implicitly done.
Inherited
Inheritance is the language of all OOP (object-oriented programming languages), including the indispensable components of the Java language.
classart{Art () {System.out.print ("Art Constructor"); } } classDrawingextendsart{Drawing () {System.out.print ("Drawing Constructor"); } } Public classCartoonextendsdrawing{ PublicCartoon () {System.out.print ("Cartoon Constructor"); } Public Static voidMain (string[] args) {Cartoon C=NewCartoon (); } }
Output:
Art Constructordrawing Constructorcartoon Constructor
In the example above, the parent class was initialized before the subclass was initialized, because the parent class's parameterless constructor was called by default when the subclass was initialized. Please note the following:
- A constructor cannot be inherited, a class can get a constructor, and there are only two ways to write a constructor, or to use the default parameterless constructor.
- If you want to invoke the constructor of a parent class with arguments , you must explicitly call the constructor of the parent class through the Super keyword in the constructor of the subclass, with the appropriate argument list (which must be placed in the first row).
- If you want to call a constructor with a parent class without arguments , it is not necessary to call the parent class constructor with super in the constructor of the subclass, and if you do not use the Super keyword, the system automatically calls the parent class's parameterless constructor.
Let's look at an example:
classart{Art (inti) {System.out.println ("Art Constructor i"); }}classDrawingextendsart{Drawing (inti) { //Super (I) constructor method that explicitly calls the base class (parent Class) Super(i); System.out.println ("Drawing Constructor i"); }} Public classCartoonextendsDrawing { PublicCartoon (inti) { //Super (I) constructor method that explicitly calls the base class (parent Class) Super(i); System.out.println ("Cartoon Constructor i"); } Public Static voidMain (string[] args) {Cartoon Cartoon=NewCartoon (1); }}
Output:
Art Constructor 111
Summarize
Both inheritance and composition can generate new types from existing types. A combination is a common reuse of an existing type as part of the underlying implementation of the new type, and the inheritance is reused as an interface.
When using inheritance, because the exported class has a base class interface, it can be moved up to the base class, which is critical for polymorphism.
Although object-oriented programming strongly emphasizes inheritance, it is generally preferable to use a combination (or possibly a proxy) when starting a design, and use inheritance only when it is really necessary. Because the combination is more flexible. In addition, you can change the type and behavior of those member objects at run time by adding techniques that use inheritance techniques to member types. Therefore, the behavior of the combined object can be changed at run time.
When designing a system, the goal should be to find or create classes where each class has a specific purpose and is neither too large (with too many functions to reuse) nor too small (unusable without adding other features). If your design becomes overly complex, it is often helpful to split existing classes into smaller parts or add more objects.
When you start designing a system, you should realize that program development is an incremental process, which is as important as human learning. Program development relies on experimentation, you can analyze it as much as you can, but when you start to execute a project, you still don't know all the answers. If the project is to be cultivated as an organic, evolutionary organism rather than as fast as a skyscraper, there will be more success and more rapid feedback. Inheritance and composition are the two basic tools that enable you to perform this experiment in object-oriented programming.
Reference:
The idea of Java programming
Java inheritance
Java Programming Thought learning notes--reuse classes
Java Learning notes-reusing classes