intermediate20 min

Interfaces and Inheritance

Explicit interface implementation with `:`, and single-class inheritance.

What you'll learn

  • Define an interface and implement it explicitly with `: IInterfaceName`
  • Derive one class from another with single inheritance
  • Contrast C#'s explicit interface implementation with an implicit, structural-typing approach

Explanation

A C# interface (conventionally prefixed I, e.g. IShape) declares a set of members a type must provide: public interface IShape { double Area(); }. C# requires a class to explicitly declare that it implements an interface: public class Rectangle : IShape { ... } -- unlike languages with implicit, structural interfaces (where having the right methods is enough), C#'s interface satisfaction is nominal: the class must name the interface.

C# also supports single-class inheritance: public class Square : Rectangle { ... } makes Square a specialized Rectangle, inheriting its public and protected members. A class can inherit from at most one base class (unlike interfaces, where a class can implement several at once: class Foo : IBar, IBaz).

A method meant to be overridden in a derived class must be marked virtual in the base class, and the derived class marks its version override -- without both keywords, the derived class's method simply hides the base one instead of genuinely overriding it (a common source of confusing bugs).

Because Square derives from Rectangle, and Rectangle implements IShape, a Square instance also satisfies IShape -- interface satisfaction flows down through the inheritance chain.

Guided lab

Predict: Interface implementation through an inheritance chain

C#Not executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

using System;

public interface IShape
{
    double Area();
}

public class Rectangle : IShape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public double Area()
    {
        return Width * Height;
    }
}

public class Square : Rectangle
{
    public Square(double side)
    {
        Width = side;
        Height = side;
    }
}

void Describe(IShape shape)
{
    Console.WriteLine($"Area: {shape.Area():F1}");
}

Rectangle rect = new Rectangle { Width = 3, Height = 4 };
Describe(rect);

Square square = new Square(5);
Describe(square);

Stuck? Get a hint.

Common mistakes

  • Expecting a class to satisfy an interface just by having matching methods -- C# requires the explicit `: IInterfaceName` declaration.
  • Trying to inherit from two classes at once (`class Foo : Bar, Baz`) -- C# allows only one base class, though a class can implement multiple interfaces.
  • Overriding a base method without marking the base version `virtual` and the derived version `override` -- without both keywords, the derived method just hides the base one rather than truly overriding it.

Knowledge check

Knowledge check

1. How does a C# class declare that it implements an interface?
2. How many classes can a single C# class directly inherit from?
3. What two keywords are both required for a derived class to genuinely override a base class method?

Takeaway

C# interface implementation is explicit (`: IInterfaceName`) -- and a derived class only truly overrides a base method when the base marks it `virtual` and the derived class marks it `override`.

Summary

Interfaces declare required members, implemented explicitly with `: IInterfaceName`; single inheritance lets a class specialize one base class; `virtual`/`override` together enable genuine method overriding.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.