Chapter 10 Object-Oriented Programming: (Web hosting colocation) Polymorphism 395 Class Shapedefines
Chapter 10 Object-Oriented Programming: Polymorphism 395 Class Shapedefines two concrete methods and one abstractproperty. All shapes have an area and a volume, so we include virtual methods Area(lines 8 11) and Volume (lines 14 17), which return the shape s area and volume, respectively. The volume of two- dimensional shapes is always zero, whereas three-dimensional shapes have a positive, nonzero volume. In class Shape, methods Area and Volume return zero, by default. Programmers can override these methods in derived classes when those classes should have different area calculations [e.g., classes Circle2 (Fig. 10.6) and Cylinder2 (Fig. 10.7)] and/or different volume calculations (e.g., Cylinder2). Read-only property Name(lines 20 23) is declared abstract, so derived classes must implement this property to become concrete classes. Note that abstractmethods and properties are implicitly virtual. Class Point2 (Fig. 10.5) inherits from abstract class Shape and overrides the abstract property Name, which makes Point2 a concrete class. A point s area and volume are zero, so class Point2 does not override base-class methods Area and Volume. Lines 59 65 implement property Name. If we did not provide this implementation, class Point2 would be an abstract class that would require keyword abstractin the first line of the class definition. 1 // Fig. 10.5: Point2.cs 2 // Point2 inherits from abstract class Shape and represents 3 // an x-y coordinate pair. 4 using System; 5 6 // Point2 inherits from abstract class Shape 7 public class Point2 : Shape 8 { 9 private int x, y; // Point2 coordinates 10 11 // default constructor 12 public Point2() 13 { 14 // implicit call to Object constructor occurs here 15 } 16 17 // constructor 18 public Point2( int xValue, int yValue ) 19 { 20 X = xValue; 21 Y = yValue; 22 } 23 24 // property X 25 public int X 26 { 27 get 28 { 29 return x; 30 } 31 Fig. 10.5 Fig. 10.5Fig. 10FiFi.5g. 10.5g. 10.5Point2class inherits from abstractclass Shape. (Part 1 of 2.)
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.