Can I create a derived class reference which points to base object?
Consider the following example. I am trying this but gives an error.
Why is it not possible?
class Shape
{
public virtual void CalculateArea()
{
//Code
}
}
class Circle: Shape
{
public override void CalculateArea()
{
//Code
}
}
//Main method
class Main()
{
Public Static void Main()
{
Circle circle = new Shape(); // WHY THIS IS NOT POSSIBLE
}
}
Is there any solution to this problem?
Because Circle is not a base class of Shape.
Yo can do this:
Shape shape = new 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