0Pricing
C# Academy · Lesson

Design (interfaces + inheritance)

Design a simple Shape API with an interface and a small base class; decide what belongs to the contract vs implementation detail.

Design (interfaces + inheritance) is a free C# Academy lesson on CoddyKit — lesson 1 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.

Project overview

Goal: Define a tiny, clear contract for shapes.

  • Use an interface for behavior
  • Keep the surface minimal
  • Allow many shapes to plug in

Contract first

Create a small IShape contract with Area() and Perimeter(). Utilities can depend on the interface.

using System;

// Interface: minimal behavior all shapes must provide
public interface IShape
{
  double Area();
  double Perimeter();
}

// A tiny helper to print any IShape
public static class ShapePrinter
{
  public static void Print(IShape s)
  {
    Console.WriteLine("A=" + s.Area() + " P=" + s.Perimeter());
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    // No concrete shapes yet; interface compiles and is ready.
    Console.WriteLine("Shape API ready");
  }
}

Optional base class

You may add a small abstract base for shared helpers, but keep the interface the official contract.

using System;

// Optional base for helpers; interface stays the main contract
public abstract class ShapeBase : IShape
{
  public abstract double Area();
  public abstract double Perimeter();

  // Shared rounding helper
  protected double Round2(double x)
  {
    return Math.Round(x, 2);
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Console.WriteLine("Base class prepared for shared helpers");
  }
}

Concrete shapes

Concrete shapes implement the interface. Clients can treat them uniformly via IShape.

using System;

public interface IShape
{
  double Area();
  double Perimeter();
}

public 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 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)
  {
    IShape a = new Rectangle(3, 4);
    IShape b = new Circle(2);
    Console.WriteLine("Rect A=" + a.Area() + " P=" + a.Perimeter());
    Console.WriteLine("Circ A=" + b.Area() + " P=" + b.Perimeter());
  }
}

Collections of IShape

Use a List<IShape> to store many shapes and process them the same way.

using System;
using System.Collections.Generic;

public interface IShape
{
  double Area();
  double Perimeter();
}

public 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 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));

    foreach (IShape s in shapes)
    {
      Console.WriteLine("A=" + s.Area() + " P=" + s.Perimeter());
    }
  }
}

Design checklist

Checklist:

  • Interface is the contract (small and clear).
  • Concrete shapes keep fields private.
  • Use doubles for simple math; keep constants local.
  • Favor polymorphism over type checks.

Contract choice

Quick check: For a shape library that must work with many different shapes, what should the public contract expose?

Recap

Recap: Define a tiny IShape interface, keep internals private, and use collections of the interface for polymorphic processing.

Frequently asked questions

Is the “Design (interfaces + inheritance)” lesson free?

Yes — the full text of “Design (interfaces + inheritance)” 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 “Design (interfaces + inheritance)”?

Design a simple Shape API with an interface and a small base class; decide what belongs to the contract vs implementation detail. 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 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Design (interfaces + inheritance)” 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

  1. Design (interfaces + inheritance)
  2. Implementation & tests
  3. Polymorphic behaviors
← Back to C# Academy