Java Inheritance and Finalize ()

Source: Internet
Author: User
Tags closure command line garbage collection inheritance throwable

When you create a new class by using the compositing method, you never have to worry about closing work on the member objects of that class. Each member is a separate object, so it gets normal garbage collection and closure-whether it's a member of a class or not. However, when initializing, the Finalize () method in the derived class must be overwritten-if a particular cleanup process has been designed, it must be done as part of the garbage collection. When overwriting the Finalize () of a derived class, be sure to remember to invoke the base class version of Finalize (). Otherwise, initialization of the underlying class does not occur at all. The following example is proof of this:
 

: Frog.java//testing Finalize with inheritance class Dobasefinalization {public static Boolean flag = FALSE;}
  Class Characteristic {String s;
    Characteristic (String c) {s = c;
  System.out.println ("Creating characteristic" + s);
  } protected void Finalize () {System.out.println ("finalizing characteristic" + s);
  } class Livingcreature {characteristic p = new characteristic ("Is Alive");
  Livingcreature () {System.out.println ("livingcreature ()");
    } protected void Finalize () {System.out.println ("livingcreature finalize");
    Call Base-class version last!
      if (Dobasefinalization.flag) try {super.finalize (); Animal extends Livingcreature {characteristic p = new characteristic ("has hea") for catch (Throwable t) {}}
  RT ");
  Animal () {System.out.println ("Animal ()");
    } protected void Finalize () {System.out.println ("Animal finalize"); if (Dobasefinalization.flagtry {super.finalize (); Amphibian extends Animal {characteristic p = new characteristic ("can live in"), catch (Throwable t) {}}}
  Water ");
  Amphibian () {System.out.println ("amphibian ()");
    } protected void Finalize () {System.out.println ("Amphibian Finalize");
      if (Dobasefinalization.flag) try {super.finalize ();
  The public class Frog extends Amphibian {Frog () {System.out.println () ("Frog ()") of catch (Throwable t) {}}};
    } protected void Finalize () {System.out.println ("Frog finalize");
      if (Dobasefinalization.flag) try {super.finalize (); The catch (Throwable t) {}} is public static void main (string[] args) {if (args.length!= 0 && args[0].
    Equals ("Finalize")) Dobasefinalization.flag = true;
    else System.out.println ("not finalizing bases"); New Frog ();
    Instantly becomes garbage System.out.println ("bye!"); Must do this to GuarantEE so all//Finalizers'll be Called:System.runFinalizersOnExit (true); }
} ///:~


The Dobasefinalization class simply accommodates a flag and indicates to each class in the hierarchy whether super.finalize () should be invoked. The setting of this flag is based on command-line parameters, so it is possible to view the behavior in the context of doing and not finishing the underlying class.
Each class in the hierarchy also contains a member object for the characteristic class. As you can see, regardless of whether or not the underlying class closure module is invoked, the characteristic member object will definitely get the finishing (cleanup) processing.
Each overwritten finalize () must have at least access to the protected member because the Finalize () method in the object class has a protected property, and the compiler does not allow us to eliminate access rights during inheritance ("friendly" is protected "has smaller access rights.)
In Frog.main (), the DOBASEFINALIZATION flag is configured and a single Frog object is created. Remember that garbage collection (especially closure) may not occur for any particular object, so in order to enforce this action, system.runfinalizersonexit (true) adds additional overhead to ensure that the finishing touches work properly. If no base class is initialized, the output results are:

Not finalizing bases
creating characteristic is Alive
livingcreature ()
creating characteristic has heart
Animal ()
Creating characteristic can live in water
amphibian () Frog
()
bye! Frog Finalize
finalizing characteristic is alive
finalizing characteristic has heart
finalizing Characteristic can live in water


You can see from this that you did not call the closure module for the underlying class frog. However, if you add a "finalize" argument to the command line, you get the following results:

Creating characteristic is Alive
livingcreature () Creating characteristic has heart Animal
()
Creating characteristic can live in water
amphibian () Frog
()
bye! Frog Finalize
Amphibian Finalize
Animal finalize
livingcreature Finalize
finalizing Characteristic is alive
finalizing characteristic has heart finalizing characteristic
can live in water


Although the member objects end up in the same order as they were created, they do not technically specify the order in which objects are ended. But for the underlying class, we can control the order of the closure. The best order to adopt is the order in which it is used, and it is exactly the reverse of the initialization order. In the same form as used in C + + for "sabotage", we should first perform the end of the derivative class, and then the end of the underlying class. This is because the end of a derived class may call the same method in the underlying class, requiring that the underlying class component is still active. Therefore, they must be cleared (destroyed) in advance.

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.