Chapter 9 Object-Oriented Programming: Inheritance 357 Fig. 9.8
Chapter 9 Object-Oriented Programming: Inheritance 357 Fig. 9.8 Fig. 9.8Fig. 9.FiFi8g. 9.8g. 9.8Circle2class that inherits from class Point. (Part 3 of 3.) Lines 14 and 20 in the Circle2constructors (lines 12 24) invoke the default Point constructor implicitly to initialize the base-class portion (variables xand y, inherited from class Point) of a Circle2object to 0. However, because the parameterized constructor (lines 18 24) should set the x-y coordinate to a specific value, lines 21 22 attempt to assign argument values to xand ydirectly. Even though lines 21 22 attempt to set xand yvalues explicitly, line 20 first calls the Point default constructor to initialize these variables to their default values. The compiler generates syntax errors for lines 21 and 22 (and line 63, where Circle2 s method ToString attempts to use the values of x and y directly), because the derived class Circle2is not allowed to access the base class Point s privatemembers xand y. C# rigidly enforces restriction on accessing privatedata members, so that even a derived class (i.e., which is closely related to its base class) cannot access base-class privatedata. To enable class Circle2 to access Point member variables x and y directly, we can declare those variables as protected. As we discussed in Section 9.3, a base class s protected members can be accessed only in that base class or in any classes derived from that base class. Class Point2 (Fig. 9.9) modifies class Point (Fig. 9.4) to declare variables xand yas protected(line 10) instead of private. 1 // Fig. 9.9: Point2.cs 2 // Point2 class contains an x-y coordinate pair as protected data. 3 4 using System; 5 6 // Point2 class definition implicitly inherits from Object 7 public class Point2 8 { 9 // point coordinate 10 protected int x, y; 11 12 // default constructor 13 public Point2() 14 { 15 // implicit call to Object constructor occurs here 16 } 17 Fig. 9.9 Fig. 9.9Fig. 9.FiFi9g. 9.9g. 9.9Point2class represents an x-y coordinate pair as protecteddata. (Part 1 of 2.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.