An explanation of the creation and destruction sequence of objects in Java containing inheritance relationships (with source code)

Source: Internet
Author: User
Tags export class

Objective
When you create a new class by combining and inheriting methods, you never have to worry about object cleanup, and the child objects are usually left to the garbage collector for processing. If you do encounter cleanup problems, you must create a new class with your heart Dispose ()Method (I choose this name here, the reader can make a better). And because of inheritance, if we have other special cleanup actions that are part of garbage collection, we must overwrite them in the export class. Dispose ()Method. When overriding an inherited class's Dispose ()method, it is important to remember to call the base class version Dispose ()Otherwise, the cleanup action of the base class will not occur. This is demonstrated in the following example:
Sample source Code
Package Com.mufeng.theeighthchapter;class characteristic {//feature private String s;public characteristic (String s) {//TODO auto-generated constructor STUBTHIS.S = s; System.out.println ("Creating characteristic" + s);} protected void Dispose () {System.out.println ("disposing characteristic" + s);}} Class Description {//description private String s;public Description (String s) {//TODO auto-generated constructor STUBTHIS.S = S;s Ystem.out.println ("Creating Description" + s);} protected void Dispose () {System.out.println ("disposing Description" + s);}} Class Livingcreature {//mob private characteristic p = new characteristic ("Is Alive");p rivate Description t = new Descripti On ("Basic Living creature");p ublic livingcreature () {//TODO auto-generated constructor StubSystem.out.println (" Livingcreature () ");} protected void Dispose () {System.out.println ("livingcreature dispose"); T.dispose ();p. Dispose ();}} Class Animal extends Livingcreature {//animal private characteristic p = new characteristic ("has Heart");p RivatE Description t = new Description ("Animal not vegetable");p ublic Animal () {//TODO auto-generated constructor STUBSYSTEM.O Ut.println ("Animal ()");} protected void Dispose () {System.out.println ("Animal dispose"); T.dispose ();p. Dispose (); Super.dispose ();}} Class Amphibian extends Animal {//amphibian private characteristic p = new characteristic ("Can live in water");p rivate descript Ion T = new Description ("Both Water and Land");p ublic amphibian () {//TODO auto-generated constructor StubSystem.out.print ln ("amphibian ()");} protected void Dispose () {System.out.println ("amphibian Dispose"); T.dispose ();p. Dispose (); Super.dispose ();}} public class Frog extends Amphibian {//frog private characteristic p = new characteristic ("Croaks");p rivate Description t = New Description ("Eats Bugs");p ublic Frog () {//TODO auto-generated constructor StubSystem.out.println ("Frog ()");} protected void Dispose () {System.out.println ("Frog dispose"); T.dispose ();p. Dispose (); Super.dispose ();} public static void Main (string[] args) {FRog frog = new Frog (); System.out.println ("bye!"); Frog.dispose ();}}


Output results
Creating characteristic is alivecreating Description Basic Living creaturelivingcreature () Creating characteristic have Heartcreating Description Animal not vegetableanimal () Creating characteristic can live in watercreating Description Both W Ater and Landamphibian () Creating characteristic croakscreating Description Eats Bugsfrog () bye! Frog disposedisposing Description Eats bugsdisposing characteristic Croaksamphibian disposedisposing Description Both Water and landdisposing characteristic can live in Wateranimal disposedisposing Description Animal not vegetabledisposing Characteristic have heartlivingcreature disposedisposing Description Basic Living creaturedisposing characteristic is Alive


SOURCE parsing
Each class in the hierarchy contains characteristicAnd DescriptionThese two types of member objects, and they must also be destroyed. So in case a child object is to be dependent on other objects, the order of destruction should be the opposite of the order of initialization. For a field, it means the opposite of the declared order (because the initialization of the field is in the order of declaration). For base classes (following C + +In the form of a destructor), you should first clean up the exported class, and then the base class. This is because the cleanup of the exported classes might call some of the methods in the base class, so the artifacts in the base class need to still work and should not be destroyed prematurely. As you can see from the output, FrogAll parts of an object are destroyed in the reverse order created. As you can see in this example, there is usually no need to perform cleanup work, but once you choose to do it, you must be cautious and careful.



An explanation of the creation and destruction sequence of objects in Java containing inheritance relationships (with source code)

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.