Archive for January, 2008

380 Object-Oriented Programming: Inheritance Chapter 9 inheritance override (Web site translator)

Tuesday, January 8th, 2008

380 Object-Oriented Programming: Inheritance Chapter 9 inheritance override keyword inheritance hierarchy overriding inherited instance variable overriding a base-class method instance variable (of an object) overriding a method internalmember access modifier private base-class member is-a relationship protected access member-access operator protected base-class member member variable (of a class) protected variable multiple inheritance protected member of a base class base reference protected member of a derived class Object class public member of a derived class object of a base class reusable component object of a derived class single inheritance object-oriented programming (OOP) software reusability overloaded constructor software reuse overloading virtual keyword SELF-REVIEW EXERCISES 9.1 Fill in the blanks in each of the following statements: a) is a form of software reusability in which new classes absorb the data and behaviors of existing classes and embellish these classes with new capabilities. b) A base class s members can be accessed only in the base-class definition or in derived-class definitions. c) In a(n) relationship, an object of a derived class also can be treated as an object of its base class. d) In a(n) relationship, a class object has one or more references to objects of other classes as members. e) A class exists in a(n) relationship with its derived classes. f) A base class s members are accessible anywhere that the program has a ref erence to that base class or to one of its derived classes. g) A base class s protected access members have a level of protection between those of publicand access. h) A base class s members can be accessed only in the same assembly. i) When an object of a derived class is instantiated, the base class s is called implicitly or explicitly to do any necessary initialization of the base-class instance variables in the derived-class object. j) Derived-class constructors can call base-class constructors via the reference. 9.2 State whether each of the following is true or false. If false, explain why. a) It is possible to treat base-class objects and derived-class objects similarly. b) Base-class constructors are not inherited by derived classes. c) A has-a relationship is implemented via inheritance. d) All methods, by default, can be overridden. e) Method ToString of class System.Object is declared as virtual. f) When a derived class redefines a base-class method using the same signature, the derived class is said to overload that base-class method. g) A Car class has an is a relationship with its SteeringWheeland Brakes. h) Inheritance encourages the reuse of proven high-quality software.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Web hosting companies - Chapter 9 Object-Oriented Programming: Inheritance 379 It

Monday, January 7th, 2008

Chapter 9 Object-Oriented Programming: Inheritance 379 It is possible to treat base-class objects and derived-class objects similarly; the commonality shared between the object types is expressed in the member variables, properties and methods of the base class. A base class s public members are accessible anywhere that the program has a reference to an object of that base class or to an object of one of that base class s derived classes. A base class s private members are accessible only within the definition of that base class. A base class s protected members have an intermediate level of protection between public and private access. A base class s protected members can be accessed only in that base class or in any classes derived from that base class. A base class s internal members can be accessed only by objects in the same assembly. Unfortunately, the inclusion of protectedinstance variables often yields two major problems. First, the derived-class object does not have to use a property to set the value of the base-class s protected data. Second, derived class methods are more likely to be written to depend on base- class implementation. C# rigidly enforces restriction on accessing private data members, so that even derived classes (i.e,. which are closely related to their base class) cannot access base-class private data. When a derived-class method overrides a base-class method, the base-class method can be accessed from the derived class by preceding the base-class method name with the base reference, followed by the dot operator (.). A derived class can redefine a base-class method using the same signature; this is called overriding that base-class method. A base-class method must be declared virtual if that method is to be overridden in a derived class. When a method is overridden in a derived class and that method is called on a derived-class object, the derived-class version (not the base-class version) is called. When an object of a derived class is instantiated, the base class s constructor is called immediately (either explicitly or implicitly) to do any necessary initialization of the base-class instance variables in the derived-class object (before the derived classes instance variable are initialized). Declaring member variables private, while providing non-private properties to manipulate and perform validation checking on this data, enforces good software engineering. If an object s method/property performs the actions needed by another object, call that method/property rather than duplicating its code body. Duplicated code creates code-maintenance problems. Base-class constructors and destructors are not inherited by derived classes. TERMINOLOGY abstraction data abstraction base class default constructor base-class constructor derived class base-class default constructor derived-class constructor base-class finalizer derived-class reference base-class object direct base class base-class reference dot (.) operator behavior garbage collector class library has-a relationship colon (:) symbol hierarchy diagram composition indirect base class constructor information hiding
Check Tomcat Web Hosting services for best quality webspace to host your web application.

378 Object-Oriented Programming: Inheritance Chapter (Net web server) 9 alternative is

Sunday, January 6th, 2008

378 Object-Oriented Programming: Inheritance Chapter 9 alternative is to create fewer classes, in which each provides more substantial functionality, but such classes might provide too much functionality. Performance Tip 9.2 If classes produced through inheritance are larger than they need to be (i.e., contain too much functionality), memory and processing resources might be wasted. Inherit from the class whose functionality is closest to what is needed. Reading derived-class definitions can be confusing, because inherited members are not shown physically in the derived class, but nevertheless are present in the derived classes. A similar problem exists when documenting derived class members. In this chapter, we introduced inheritance the ability to create classes by absorbing an existing class s data members and behaviors and embellishing these with new capabilities. In Chapter 10, we build upon our discussion of inheritance by introducing polymorphism an object-oriented technique that enables us to write programs that handle, in a more general manner, a wide variety of classes related by inheritance. After studying Chapter 10, you will be familiar with encapsulation, inheritance and polymorphism the most crucial aspects of object-oriented programming. SUMMARY Software reusability reduces program-development time. The direct base class of a derived class is the base class from which the derived class inherits [via the colon (:) symbol]. An indirect base class of a derived class is two or more levels up the class hierarchy from that derived class. With single inheritance, a class is derived from one base class. C# does not support multiple inheritance (i.e., deriving a class from more than one direct base class). Because a derived class can include its own class variables, properties and methods, a derived class is often larger than its base class. A derived class is more specific than its base class and represents a smaller group of objects. Every object of a derived class is also an object of that class s base class. However, base-class objects are not objects of that class s derived classes. Derived-class methods and properties can access protected base-class members directly. An is-a relationship represents inheritance. In an is-a relationship, an object of a derived class also can be treated as an object of its base class. A has-a relationship represents composition. In a has-a relationship, a class object has references to one or more objects of other classes as members. A derived class cannot access private members of its base class directly. A derived class can access the public, protectedand internal members of its base class if the derived class is in the same assembly as the base class. When a base-class member is inappropriate for a derived class, that member can be overridden (redefined) in the derived class with an appropriate implementation. To override a base-class method definition, a derived class must specify that the derived-class method overrides the base-class method with keyword override in the method header. Inheritance relationships form tree-like hierarchical structures. A class exists in a hierarchical relationship with its derived classes.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Chapter 9 Object-Oriented Programming: Inheritance 377 Line 17

Saturday, January 5th, 2008

Chapter 9 Object-Oriented Programming: Inheritance 377 Line 17 instantiates another object of class Circle5, then assigns it to reference circle2. Again, this begins the chain of constructor calls in which the Circle5 constructor, the Point4 constructor and the Object constructor are called. In the output, notice that the body of the Point4constructor executes before the body of the Circle5 constructor. This demonstrates that objects are constructed inside out (i.e., the base-class constructor is called first). Lines 22 23 set references circle1 and circle2to null. This removes the only references to these Circle5 objects in the program. Thus, the garbage collector can release the memory that these objects occupy. Remember that we cannot guarantee when the garbage collector will execute, nor can we guarantee that it will collect all available objects when it does execute. To demonstrate the destructor invocations for the two Circle5 objects, line 26 invokes class GC s method Collect to request the garbage collector to run. Notice that each Circle5 object s destructor outputs information before calling class Point4 s destructor. Objects are destroyed outside in (i.e., the derived- class destructor completes its tasks before invoking the base-class destructor). 9.7 Software Engineering with Inheritance In this section, we discuss the use of inheritance to customize existing software. When we use inheritance to create a new class from an existing one, the new class inherits the member variables, properties and methods of the existing class. We can customize the new class to meet our needs by including additional member variables, properties and methods, and by overriding base-class members. Sometimes, it is difficult for students to appreciate the scope of problems faced by designers who work on large-scale software projects in industry. People experienced with such projects say that effective software reuse improves the software-development process. Object-oriented programming facilitates software reuse, thus shortening development times. C# encourages software reuse by providing the .NET Framework Class Library (FCL), which delivers the maximum benefits of software reuse through inheritance. As interest in C# grows, interest in the FCL class libraries also increases. There is a worldwide commitment to the continued evolution of the FCL class libraries for a wide variety of applications. The FCL will grow as the .NET world grows explosively. Software Engineering Observation 9.11 At the design stage in an object-oriented system, the designer often determines that certain classes are closely related. The designer should factor out common attributes and behaviors and place these in a base class. Then, use inheritance to form derived classes, endowing them with capabilities beyond those inherited from the base class. Software Engineering Observation 9.12 The creation of a derived class does not affect its base class s source code. Inheritance preserves the integrity of a base class. Software Engineering Observation 9.13 Just as designers of non-object-oriented systems should avoid proliferation of functions, designers of object-oriented systems should avoid proliferation of classes. Proliferation of classes creates management problems and can hinder software reusability, because it becomes difficult for a client to locate the most appropriate class of a huge class library. The
Check Tomcat Web Hosting services for best quality webspace to host your web application.

376 Object-Oriented Programming: Inheritance Chapter 9 Circle5object being (Database web hosting)

Friday, January 4th, 2008

376 Object-Oriented Programming: Inheritance Chapter 9 Circle5object being constructed, Circle5 s ToStringmethod executes even when ToString is invoked from the body of class Point4 s constructor. [Note: This would not be the case if the Point4constructor were called to initialize a an object that was actually a new Point4 object.] When the Point4 constructor invokes method ToString for the Circle5 being constructed, the program displays 0 for the radius value, because the Circle5 constructor s body has not yet initialized the radius. Remember that 0 is the default value of a double variable. The second line of output shows the proper radiusvalue (4.5), because that line is output after the radiusis initialized. 1 // Fig. 9.19: ConstructorAndDestructor.cs 2 // Display order in which base-class and derived-class constructors 3 // and destructors are called. 4 5 using System; 6 7 // ConstructorAndFinalizer class definition 8 class ConstructorAndFinalizer 9 { 10 // main entry point for application. 11 static void Main( string[] args ) 12 { 13 Circle5 circle1, circle2; 14 15 // instantiate objects 16 circle1 = new Circle5( 72, 29, 4.5 ); 17 circle2 = new Circle5( 5, 5, 10 ); 18 19 Console.WriteLine(); 20 21 // mark objects for garbage collection 22 circle1 = null; 23 circle2 = null; 24 25 // inform garbage collector to execute 26 System.GC.Collect(); 27 28 } // end method Main 29 30 } // end class ConstructorAndDestructor Point4 constructor: Center = [72, 29]; Radius = 0 Circle5 constructor: Center = [72, 29]; Radius = 4.5 Point4 constructor: Center = [5, 5]; Radius = 0 Circle5 constructor: Center = [5, 5]; Radius = 10 Circle5 destructor: Center = [5, 5]; Radius = 10 Point4 destructor: Center = [5, 5]; Radius = 10 Circle5 destructor: Center = [72, 29]; Radius = 4.5 Point4 destructor: Center = [72, 29]; Radius = 4.5 Fig. 9.19 Fig. 9.19Fig. 9.FiFi19g. 9.19g.9.19Order in which constructors and destructors are called.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Chapter 9 Object-Oriented Programming: Inheritance (Web server iis) 375 31 32

Thursday, January 3rd, 2008

Chapter 9 Object-Oriented Programming: Inheritance 375 31 32 // property Radius 33 public double Radius 34 { 35 get 36 { 37 return radius; 38 } 39 40 set 41 { 42 if ( value >= 0 ) 43 radius = value; 44 } 45 46 } // end property Radius 47 48 // calculate Circle5 diameter 49 public double Diameter() 50 { 51 return Radius * 2; 52 } 53 54 // calculate Circle5 circumference 55 public double Circumference() 56 { 57 return Math.PI * Diameter(); 58 } 59 60 // calculate Circle5 area 61 public virtual double Area() 62 { 63 return Math.PI * Math.Pow( Radius, 2 ); 64 } 65 66 // return string representation of Circle5 67 public override string ToString() 68 { 69 // use base reference to return Point3 string 70 return “Center = ” + base.ToString() + 71 “; Radius = ” + Radius; 72 } 73 74 } // end class Circle5 Fig. 9.18 Fig. 9.18Fig. 9.FiFi18g. 9.18g. 9.18Circle5class inherits from class Point3and overrides a finalizer method. (Part 2 of 2.) Notice that the first two lines of the output from this program contain values for the x- y coordinates and the radius of Circle5 object circle1. When constructing a Circle5object, the thisreference used in the body of both the Circle5and Point4 constructors refers to the Circle5 object being constructed. When a program invokes method ToString on an object, the version of ToString that executes is always the version defined in that object s class. Because reference this refers to the current
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

374 Object-Oriented Programming: Inheritance Chapter 9 Class Circle5(Fig. (Web hosting faq)

Thursday, January 3rd, 2008

374 Object-Oriented Programming: Inheritance Chapter 9 Class Circle5(Fig. 9.18) contains the features in Fig. 9.13, and we modified the two constructors (lines 12 16 and 19 24) to output a line of text when they are called. We also added a destructor (lines 27 30) that also outputs a line of text when it is called. Each output statement (lines 15, 23 and 29) adds reference this to the output string. This implicitly invokes the Circle5 s ToString method to obtain the string representation of Circle5 s coordinates and radius. Class ConstructorAndFinalizer (Fig. 9.19) demonstrates the order in which constructors and finalizers are called for objects of classes that are part of an inheritance class hierarchy. Method Main (lines 11 28) begins by instantiating an object of class Circle5, then assigns it to reference circle1 (line 16). This invokes the Circle5 constructor, which invokes the Point4constructor immediately. Then, the Point4constructor invokes the Objectconstructor. When the Objectconstructor (which does not print anything) returns control to the Point4constructor, the Point4constructor initializes the x-y coordinates, then outputs a string indicating that the Point4 constructor was called. The output statement also calls method ToStringimplicitly (using reference this) to obtain the stringrepresentation of the object being constructed. Then, control returns to the Circle5 constructor, which initializes the radius and outputs the Circle5 s x-y coordinates and radius by calling method ToStringimplicitly. 1 // Fig. 9.18: Circle5.cs 2 // Circle5 class that inherits from class Point4. 3 4 using System; 5 6 // Circle5 class definition inherits from Point4 7 public class Circle5 : Point4 8 { 9 private double radius; 10 11 // default constructor 12 public Circle5() 13 { 14 // implicit call to Point3 constructor occurs here 15 Console.WriteLine( “Circle5 constructor: {0}”, this ); 16 } 17 18 // constructor 19 public Circle5( int xValue, int yValue, double radiusValue ) 20 : base( xValue, yValue ) 21 { 22 Radius = radiusValue; 23 Console.WriteLine( “Circle5 constructor: {0}”, this ); 24 } 25 26 // destructor overrides version in class Point4 27 ~Circle5() 28 { 29 Console.WriteLine( “Circle5 destructor: {0}”, this ); 30 } Fig. 9.18 Fig. 9.18Fig. 9.FiFi18g. 9.18g. 9.18Circle5class inherits from class Point3and overrides a finalizer method. (Part 1 of 2.)
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Chapter 9 Object-Oriented Programming: Inheritance 373 18 19 (Cedant web hosting)

Wednesday, January 2nd, 2008

Chapter 9 Object-Oriented Programming: Inheritance 373 18 19 // constructor 20 public Point4( int xValue, int yValue ) 21 { 22 // implicit call to Object constructor occurs here 23 X = xValue; 24 Y = yValue; 25 Console.WriteLine( “Point4 constructor: {0}”, this ); 26 } 27 28 // destructor 29 ~Point4() 30 { 31 Console.WriteLine( “Point4 destructor: {0}”, this ); 32 } 33 34 // property X 35 public int X 36 { 37 get 38 { 39 return x; 40 } 41 42 set 43 { 44 x = value; // no need for validation 45 } 46 47 } // end property X 48 49 // property Y 50 public int Y 51 { 52 get 53 { 54 return y; 55 } 56 57 set 58 { 59 y = value; // no need for validation 60 } 61 62 } // end property Y 63 64 // return string representation of Point4 65 public override string ToString() 66 { 67 return “[” + x + “, ” + y + “]”; 68 } 69 70 } // end class Point4 Fig. 9.17 Fig. 9.17Fig. 9.FiFi17g. 9.17g. 9.17Point4base class contains constructors and finalizer. (Part 2 of 2.)
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

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

Tuesday, January 1st, 2008

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.