368 Object-Oriented Programming: Inheritance Chapter 9 9.5 Case (Msn web hosting)

368 Object-Oriented Programming: Inheritance Chapter 9 9.5 Case Study: Three-Level Inheritance Hierarchy Let us consider a more substantial inheritance example involving a three-level point-circlecylinder hierarchy. In Section 9.4, we developed classes Point3 (Fig. 9.12) and Circle4(Fig. 9.13). Now, we present an example in which we derive class Cylinder from class Circle4. The first class that we use in our case study is class Point3(Fig. 9.12). We declared Point3 s instance variables as private. Class Point3also contains properties Xand Y for accessing x and y and method ToString (which Point3 overrides from class Object) for obtaining a string representation of the x-y coordinate pair. We also created class Circle4 (Fig. 9.13), which inherits from class Point3. Class Circle4 contains the Point3 functionality, in addition to providing property Radius, which ensures that the radiusmember variable cannot hold a negative value, and methods Diameter, Circumference, Area and ToString. Recall that method Area was declared virtual(line 53). As we discussed in Section 9.4, this keyword enables derived classes to override a base-class method. Derived classes of class Circle4 (such as class Cylinder, which we introduce momentarily) can override these methods and provide specific implementations. A circle has an area that is calculated by the formula, pr2, in which r represents the circle s radius. However, a cylinder has a surface area that is calculated by the formula, (2pr2) + (2prh), in which r represents the cylinder s radius and h represents the cylinder s height. Therefore, class Cylindermust override method Areato include this calculation, so we declared class Circle4 s method Areaas virtual. Figure 9.15 presents class Cylinder, which inherits from class Circle4 (line 7). Class Cylinder s public services include the inherited Circle4 methods Diameter, Circumference, Area and ToString; the inherited Circle4 property Radius; the indirectly inherited Point3properties Xand Y; the Cylinderconstructor, property Height and method Volume. Method Area (lines 41 44) overrides method Area of class Circle4. Note that, if class Cylinder were to attempt to override Circle4 s methods Diameter and Circumference, syntax errors would occur, because class Circle4 did not declare these methods virtual. Method ToString (lines 53 56) overrides method ToStringof class Circle4to obtain a stringrepresentation for the cylinder. Class Cylinder also includes method Volume (lines 47 50) to calculate the cylinder s volume. Because we do not declare method Volume as virtual, no derived class of class Cylindercan override this method. 1 // Fig. 9.15: Cylinder.cs 2 // Cylinder class inherits from class Circle4. 3 4 using System; 5 6 // Cylinder class definition inherits from Circle4 7 public class Cylinder : Circle4 8 { 9 private double height; 10 Fig. 9.15 Fig. 9.15Fig. 9.FiFi15g. 9.15g. 9.15Cylinderclass inherits from class Circle4and overrides method Area. (Part 1 of 2.)
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Leave a Reply