Polymorphism
Call different implementations through one shared contract.
Introduction
Polymorphism lets calling code work with an abstraction instead of branching on concrete types.
Each object selects its own override at runtime even though the loop variable uses the common Shape type.
Runnable C# example
Program.cs
using System;
abstract class Shape { public abstract double Area(); }
class Circle(double radius) : Shape { public override double Area() => Math.PI * radius * radius; }
class Square(double side) : Shape { public override double Area() => side * side; }
class Program
{
static void Main()
{
Shape[] shapes = { new Circle(2), new Square(3) };
foreach (Shape shape in shapes) Console.WriteLine($"{shape.Area():F2}");
}
}
12.57
9.00Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges