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. This is 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 3 lessons in total.
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.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Polymorphic behaviors” lesson free?
Yes — the full text of “Polymorphic behaviors” is free to read here on the web. 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. The C# Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.
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