Polymorphic behaviors
Use interface-based polymorphism: process mixed shapes uniformly, add new shapes without changing callers, and compose small behaviors.
Polymorphic behaviors is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Polymorphism overview
Goal: Use polymorphism to keep callers simple.
- Work with IShape everywhere
- Mix many shapes in one list
- Add new shapes without changing code
- Compose tiny behaviors (formatting, filtering)
One API for all shapes
Accept the IShape interface in helpers. Runtime dispatch picks the right implementation.
using System;
public interface IShape
{
double Area();
double Perimeter();
}
public sealed class Rectangle : IShape
{
private readonly double _w, _h;
public Rectangle(double w, double h) { _w = w; _h = h; }
public double Area() { return _w * _h; }
public double Perimeter() { return 2 * (_w + _h); }
}
public sealed class Circle : IShape
{
private readonly double _r;
public Circle(double r) { _r = r; }
public double Area() { return 3.14159 * _r * _r; }
public double Perimeter() { return 2 * 3.14159 * _r; }
}
public class Program
{
// One function works for any IShape (dynamic dispatch on overrides)
static void PrintSummary(IShape s)
{
Console.WriteLine("A=" + s.Area() + " P=" + s.Perimeter());
}
public static void Main(string[] args)
{
PrintSummary(new Rectangle(3, 4));
PrintSummary(new Circle(2));
}
}
Mixed list processing
Store many shapes in a List<IShape>. Iteration stays simple while behavior varies per instance.
using System;
using System.Collections.Generic;
public interface IShape { double Area(); double Perimeter(); }
public sealed class Rectangle : IShape
{
private readonly double _w, _h;
public Rectangle(double w, double h) { _w = w; _h = h; }
public double Area() { return _w * _h; }
public double Perimeter() { return 2 * (_w + _h); }
}
public sealed class Circle : IShape
{
private readonly double _r;
public Circle(double r) { _r = r; }
public double Area() { return 3.14159 * _r * _r; }
public double Perimeter() { return 2 * 3.14159 * _r; }
}
public class Program
{
public static void Main(string[] args)
{
List<IShape> shapes = new List<IShape>();
shapes.Add(new Rectangle(2, 5));
shapes.Add(new Circle(1.5));
shapes.Add(new Rectangle(1, 1));
foreach (IShape s in shapes)
{
Console.WriteLine("A=" + s.Area());
}
}
}
Open for extension
Polymorphism supports open extension: new shapes integrate without editing existing callers.
using System;
public interface IShape { double Area(); double Perimeter(); }
public sealed class Triangle : IShape
{
private readonly double _a, _b, _c;
public Triangle(double a, double b, double c) { _a = a; _b = b; _c = c; }
public double Perimeter() { return _a + _b + _c; }
public double Area()
{
double s = (_a + _b + _c) / 2.0;
return Math.Sqrt(s * (s - _a) * (s - _b) * (s - _c));
}
}
public class Program
{
// Existing helper still works; no changes needed
static void Show(IShape s) { Console.WriteLine("A=" + s.Area() + " P=" + s.Perimeter()); }
public static void Main(string[] args)
{
Show(new Triangle(3, 4, 5)); // new type plugs in
}
}
Behavior composition (strategy)
Compose tiny behaviors via small interfaces (like a formatter). Swap strategies without changing the shape.
using System;
public interface IShape { double Area(); double Perimeter(); }
public sealed class Rectangle : IShape
{
private readonly double _w, _h;
public Rectangle(double w, double h) { _w = w; _h = h; }
public double Area() { return _w * _h; }
public double Perimeter() { return 2 * (_w + _h); }
}
// Small strategy interface for formatting
public interface IShapeFormatter
{
string Format(IShape s);
}
public sealed class SimpleFormatter : IShapeFormatter
{
public string Format(IShape s) { return "A=" + s.Area() + " P=" + s.Perimeter(); }
}
public class Program
{
static void Print(IShape s, IShapeFormatter f)
{
Console.WriteLine(f.Format(s));
}
public static void Main(string[] args)
{
Print(new Rectangle(3, 2), new SimpleFormatter());
}
}
Polymorphism tips
Tips:
- Pass IShape to helpers; avoid type checks.
- Keep helpers tiny and pure when possible.
- Add new shapes freely; do not edit existing callers.
- Use small behavior interfaces (formatting, filtering) to extend features.
Interface benefit
Recap
Recap: Take parameters as IShape, process mixed lists, and add shapes without touching callers. Compose small behaviors for formatting or filtering to keep code flexible.
Frequently asked questions
Is the “Polymorphic behaviors” lesson free?
Yes — the full text of “Polymorphic behaviors” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Polymorphic behaviors”?
Use interface-based polymorphism: process mixed shapes uniformly, add new shapes without changing callers, and compose small behaviors. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Polymorphic behaviors” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Design (interfaces + inheritance)
- Implementation & tests
- Polymorphic behaviors