Hard csharp

OOP — Polymorphism with virtual #5

Problem

Base Shape.Area() overridden in Circle.

Hints
  • abstract override Area()

Your practice code

Ready — edit the code above and click Run.

Solution

using System;

class Program
{
    static void Main()
    {
        Shape s = new Circle(3);
        Console.WriteLine(s.Area());

abstract class Shape { public abstract double Area(); }
class Circle : Shape {
    double r; public Circle(double r) => this.r = r;
    public override double Area() => Math.PI * r * r;
}
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Runtime polymorphism—virtual/override or abstract methods.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details