4.3 Inheritance: the Circle derived class
-
Change the class definition of
Circleas follows:1 2
public class Circle : Shape {The code
: Shapesays that theCircleclass is now a derived class of theShapeclass. All attributes and methods that are public or protected within theShapeclass are now also available to theCircleclass. -
Remove the line:
The constant is now inherited from the1private const double LimitValueForBigShape = 100.0;Shapeclass. -
Remove the line:
The attribute is also inherited from the1private string _color;Shapeclass. -
Modify the all-args constructor as follows:
1 2 3 4 5
public Circle(double radius, Point center, string color) : base(color) { _radius = radius; _center = center; }coloris an attribute of theShapeclass.baserefers to the base (super) class.base(color)calls the all-args constructor of theShapeclass. -
Modify the radius constructor as follows:
1 2 3
public Circle(double radius) : this(radius, new Point(), DefaultColor) { }The
Shapeclass determines the default value for color. This is used in the constructor chaining. -
Add the
overridekeyword to theCalculatePerimeter()andCalculateArea()methods:1 2 3 4 5 6 7 8 9
public override double CalculatePerimeter() { return 2 * Math.PI * _radius; } public override double CalculateArea() { return Math.PI * _radius * _radius; }overrideindicates that the original methods in theShapeclass are not used in the derivedCircleclass. Instead, the class's own methods are used. Note that the keyword is optional for correctness but improves readability. -
Remove the entire
DescribeSize()method from theCircleclass. The method in theShapeclass is now used and it is not necessary to have a separate implementation. -
Remove the entire
Colorgetter and setter. These are also inherited from theShapeclass. -
In the
Mainmethod, go to the line withConsole.WriteLine(myCircleArray[i].Center.Y);and add an empty line. -
Type
Console.WriteLine(myCircleArray[i]and then a period. You will see that the methodDescribeSize()is available in theCircleclass. AlsoColorgetter and setter are available. -
Select the method
DescribeSize(). -
Comment out the entire code about the rectangle.