Given the following code, I have inherited a class Circle from Shape:
class Shape
{
void Draw();
}
class Circle : Shape
{
}
void Main(string[] args)
{
Shape s = new Shape();
Shape s2 = new Shape();
Circle c = new Circle();
List<Shape> ShapeList = new List<Shape>();
ShapeList.Add(s);
ShapeList.Add(s2);
ShapeList.Add(c);
}
How can c
be added into the ShapeList
?
A Circle
is a Shape
, because Circle
extends Shape
. Because of that, you can always treat a Circle
object as if it were a Shape
since we can be absolutely sure that all of the operations that can be performed on a Shape
can also be performed on a Circle
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With