Inheritance and interfaces
A base class holds what related classes share; a derived class inherits it with a colon and overrides what must differ. An interface is a contract any class can sign. Follow one small program: Square and Rect inherit from the abstract class Shape, which implements IShape; the same line of code prints Square: 9 and then Rect: 10, because each call runs the override of the object's real type.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01class Square(double side) : Shape("Square") — what does the part after the colon mean?
The colon names the base class. A Square is a Shape: it inherits Name and Info(), and Shape("Square") passes an argument to Shape's (primary) constructor.
02shapes is a Shape[] holding a Square and a Rect. In the loop, s.Info() calls Area(). Which Area() runs for the Rect?
For a virtual (or abstract) method, the run-time type of the object decides which override runs, not the declared type of the variable. So the line prints Rect: 10.
03Why does new Shape("x") not compile?
An abstract class is incomplete (Area() has no body), so C# forbids creating one directly. You create derived classes that fill in the missing parts.
04What can an interface such as IShape NOT contain?
An interface is a contract: it lists members a type must have. It has no instance fields. (Since C# 8 it may hold default method bodies and static members.) Starting the name with I is only a naming convention.
05Which base list (the part after the colon) does C# allow?
A class has exactly one base class, written first, followed by any number of interfaces (the class must then implement every interface member). Two base classes are not allowed, the base class must come before the interfaces, and an interface cannot inherit from a class.
Glossary
- Base class
- The class another class inherits from, such as Shape. It holds the members its derived classes share. Every class ultimately derives from object.
- Derived class
- A class that inherits from a base class with a colon, such as Square : Shape. It gets the base members and can add and override its own.
- Abstract
- A class marked abstract cannot be created with new; an abstract method has no body and every non-abstract derived class must override it.
- Virtual and override
- A virtual (or abstract) method can be replaced in a derived class with override. The object's real type decides which version runs.
- Polymorphism
- One call, different behaviour: the same line s.Info() runs Square's or Rect's Area() depending on the object s refers to.
- Interface
- A contract: a named list of members (methods, properties) a type promises to have, with no instance fields. A class can implement many.
- Reference
- What a class-type variable holds: a way to reach an object on the heap. Assigning it to a Shape or IShape variable copies the reference, not the object.
Notes and sources
- The code cards leave out each type's braces { } and the indentation. The full Program.cs is shown on this page; it builds with no warnings on .NET 8 (C# 12), which added primary constructors and collection expressions.
- Square(double side) is a primary constructor. Because Area() uses side, the compiler stores it in a hidden private field of the object; the figure simply calls that field side. Name is a property with its own hidden backing field.
- Every .NET object also carries a small header and a pointer to its type's method table, which is how the runtime finds the right override. The figure shows only the fields.
- When an object is built, the derived class's field initialisers run, then the base constructor, then the derived constructor. The figure shows both parts appearing together.
- Since C# 8 an interface may also contain default method bodies and static members, but never instance fields.
- Run it with dotnet run and it prints three lines: Square: 9, Rect: 10, 10.
- Microsoft Learn: Inheritance (C# and .NET)
- Microsoft Learn: Polymorphism
- Microsoft Learn: abstract (C# reference)
- Microsoft Learn: override (C# reference)
- Microsoft Learn: Interfaces - define behavior for multiple types
- Microsoft Learn: interface (C# reference)
- Microsoft Learn: Declare primary constructors for classes and structs
- Microsoft Learn: Collection expressions
- Microsoft Learn: Reference types (C# reference)
- Microsoft Learn: Casting and type conversions
Quiz Unlocks after the last step.