372 Object-Oriented Programming: Inheritance (Web site template) Chapter 9 Software Engineering

372 Object-Oriented Programming: Inheritance Chapter 9 Software Engineering Observation 9.10 When a program creates a derived-class object, the derived-class constructor immediately calls the base-class constructor, the base-class constructor s body executes, then the de- rived-class constructor s body executes. When the garbage collector removes a derived-class object from memory, the garbage collector calls that object s destructor. This begins a chain of destructor calls in which the derived-class destructor and the destructors of the direct and indirect base classes execute in the reverse order of the order in which the constructors executed. Executing the destructors should free all the resources the object acquired before the garbage collector reclaims the memory for that object. When the garbage collector calls a derived-class object s destructor, the destructor performs its task, then invokes the destructor of the base class. This process repeats until class Object s destructor is called. C# actually implements destructors using class Object s Finalize method (one of the eight methods that every C# class inherits). When compiling a class definition that contains a destructor, the compiler translates a destructor definition into a Finalizemethod that performs the destructor s tasks, then invokes the base class Finalizemethod as the last statement in the derived-class Finalize method. As mentioned in Chapter 8, we cannot determine exactly when the destructor call will occur, because we cannot determine exactly when garbage collection occurs. However, by defining a destructor, we can specify code to execute before the garbage collector removes an object from memory. Our next example revisits the point-circle hierarchy by defining class Point4 (Fig. 9.17) and class Circle5 (Fig. 9.18) that contain constructors and destructors, each of which prints a message when it runs. Class Point4(Fig. 9.17) contains the features shown in Fig. 9.4. We modified the constructors (lines 13 17 and 20 26) to output a line of text when they are called and added a destructor (lines 29 32) that also outputs a line of text when it is called. Each output statement (lines 16, 25 and 31) adds reference this to the output string. This implicitly invokes the class s ToStringmethod to obtain the stringrepresentation of Point4 s coordinates. 1 // Fig. 9.17: Point4.cs 2 // Point4 class represents an x-y coordinate pair. 3 4 using System; 5 6 // Point4 class definition 7 public class Point4 8 { 9 // point coordinate 10 private int x, y; 11 12 // default constructor 13 public Point4() 14 { 15 // implicit call to Object constructor occurs here 16 Console.WriteLine( “Point4 constructor: {0}”, this ); 17 } Fig. 9.17 Fig. 9.17Fig. 9.FiFi17g. 9.17g. 9.17Point4base class contains constructors and finalizer. (Part 1 of 2.)
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Leave a Reply