Skip to main content

How to Inherit from an Abstract Class and Override Its Abstract Methods in C#

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

Mark a class abstract, and C# won't let anyone create it directly: it exists to be inherited. Mark a method in it abstract and leave out its body, and every class that derives from it has to supply that method with override.

Microsoft's reference: The abstract modifier indicates that its target has a missing or incomplete implementation. For a class: Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes, not instantiated on its own. Non-abstract classes that derive from the abstract class must implement members marked as abstract.

An abstract class can still hold working code. The same reference: Abstract classes can contain both abstract members (which have no implementation and must be overridden in derived classes) and fully implemented members (such as regular methods, properties, and constructors).

Syntax​

Syntax
abstract class <Base>
{
public abstract <ReturnType> <Method>(<parameters>);
}

class <Derived> : <Base>
{
public override <ReturnType> <Method>(<parameters>) => <expression>;
}
PartWhat it is
abstract class <Base>A class that can't be created with new. Other classes derive from it
public abstract <ReturnType> <Method>(<parameters>);An abstract method: a return type, a name and parameters, with ; where the body would be
class <Derived> : <Base>A class that derives from <Base>. Unless it's abstract too, it must override every abstract method
public override <ReturnType> <Method>(<parameters>)The derived class's version, with a body. The override reference: An override method must have the same signature as the overridden base method.
=> <expression>A body that is one expression, whose value the method returns. A body in braces works too

On the missing body, the abstract reference: Because an abstract method declaration provides no actual implementation, there's no method body. The method declaration simply ends with a semicolon.

The Example​

A console app called AbstractShapes has an abstract Shape class with two abstract methods, Area and Perimeter, and one ordinary method, Describe, that uses them. Circle and Rectangle derive from Shape and override both abstract methods. The app puts a circle and a rectangle in one array and describes each.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n AbstractShapes
cd AbstractShapes
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/AbstractShapes/AbstractShapes.csproj:
Determining projects to restore...
Restored /Users/you/source/AbstractShapes/AbstractShapes.csproj (in 45 ms).
Restore succeeded.

dotnet new console -n AbstractShapes creates a folder named AbstractShapes with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or AbstractShapes.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. The classes go at the end of the file. First, the abstract class:

Program.cs (the abstract class)
// The base class: two abstract methods with no body, and one ordinary method.
abstract class Shape
{
public abstract double Area();
public abstract double Perimeter();

public string Describe() => $"area {Area():F2}, perimeter {Perimeter():F2}";
}

Area and Perimeter are abstract: each has a return type and a name, and ; where the body would be. Describe is an ordinary method with a body, and :F2 writes each number with two decimal places. Describe calls Area() and Perimeter() without knowing how any shape works them out.

Then the two shapes:

Program.cs (the shapes)
// Each class that derives from Shape overrides both abstract methods.
class Circle(double radius) : Shape
{
public override double Area() => Math.PI * radius * radius;
public override double Perimeter() => 2 * Math.PI * radius;
}

class Rectangle(double width, double height) : Shape
{
public override double Area() => width * height;
public override double Perimeter() => 2 * (width + height);
}

: Shape makes each class derive from Shape, and override gives each abstract method a body. Circle(double radius) is a primary constructor: new Circle(3) passes 3 as radius. The primary constructors guide: Primary constructor parameters are in scope throughout the class definition. Math.PI is π. The abstract reference: The implementation is provided by a method override, which is a member of a non-abstract class.

At the top of the file, use them:

Program.cs (use the shapes)
// Shape is abstract: you can't create one, but a Shape variable can hold any shape.
Shape[] shapes = [new Circle(3), new Rectangle(3, 4)];

foreach (Shape shape in shapes)
{
// Describe is written once, in Shape, and it calls each shape's own overrides.
Console.WriteLine($"{shape.GetType().Name}: {shape.Describe()}");
}

The square brackets make an array of the two shapes, and because its type is Shape[], it holds both a Circle and a Rectangle. shape.GetType().Name gives the name of each object's own class, and shape.Describe() runs the one Describe in Shape.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Circle: area 28.27, perimeter 18.85
Rectangle: area 12.00, perimeter 14.00

macOS-style terminal window titled Terminal — AbstractShapes showing dotnet run printing Circle: area 28.27, perimeter 18.85, then Rectangle: area 12.00, perimeter 14.00

  • Circle: with a radius of 3, its overrides gave an area of 28.27 and a perimeter of 18.85.
  • Rectangle: 3 by 4 gave an area of 12.00 and a perimeter of 14.00.

The same Describe method in Shape printed both lines, and each Area() and Perimeter() call inside it ran the override of the shape it was called on. The reference: An abstract method is implicitly a virtual method.

The Complete File​

macOS-style code window titled Program.cs — AbstractShapes showing an array of Shape holding a new Circle with radius 3 and a new Rectangle 3 by 4, a loop printing each shape's class name and Describe(), the abstract Shape class with abstract Area and Perimeter methods and an ordinary Describe method, and the Circle and Rectangle classes overriding Area and Perimeter

Program.cs
// Shape is abstract: you can't create one, but a Shape variable can hold any shape.
Shape[] shapes = [new Circle(3), new Rectangle(3, 4)];

foreach (Shape shape in shapes)
{
// Describe is written once, in Shape, and it calls each shape's own overrides.
Console.WriteLine($"{shape.GetType().Name}: {shape.Describe()}");
}

// The base class: two abstract methods with no body, and one ordinary method.
abstract class Shape
{
public abstract double Area();
public abstract double Perimeter();

public string Describe() => $"area {Area():F2}, perimeter {Perimeter():F2}";
}

// Each class that derives from Shape overrides both abstract methods.
class Circle(double radius) : Shape
{
public override double Area() => Math.PI * radius * radius;
public override double Perimeter() => 2 * Math.PI * radius;
}

class Rectangle(double width, double height) : Shape
{
public override double Area() => width * height;
public override double Perimeter() => 2 * (width + height);
}

Common Mistakes​

MistakeWhat happensDo this instead
Creating the abstract class itself: new Shape()error CS0144: Cannot create an instance of the abstract type or interface 'Shape'Create a class that derives from it, such as new Circle(3), and keep it in a Shape variable if you like
Leaving out an overrideA Rectangle without Perimeter failed with error CS0534: 'Rectangle' does not implement inherited abstract member 'Shape.Perimeter()'Override every abstract method the class inherits
Writing the method without overrideThe build warned warning CS0114: 'Rectangle.Perimeter()' hides inherited member 'Shape.Perimeter()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. and failed with the same error CS0534Write public override double Perimeter()
Giving an abstract method a bodyerror CS0500: 'Shape.Area()' cannot declare a body because it is marked abstractEnd it with ;, and put the body in each override

FAQ​

Does every class that derives from it have to override every abstract method?​

Only a class that isn't abstract itself. An abstract Polygon that derives from Shape built without overriding Area or Perimeter, and added an abstract Corners method of its own. A Square that derives from Polygon then overrode all three, and printed Square: area 25.00, perimeter 20.00, 4 corners. The reference: A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

Can an ordinary class have an abstract method?​

No. Without abstract on class Shape, each abstract method failed with an error such as error CS0513: 'Shape.Area()' is abstract but it is contained in non-abstract type 'Shape'. The reference: Abstract method declarations are only permitted in abstract classes.

Can an override call the abstract method with base?​

No, because the abstract method has no body to run. base.Area() inside Circle's override failed with error CS0205: Cannot call an abstract base member: 'Shape.Area()'. The override has to do the whole job itself.

Conclusion​

Mark a class abstract when it exists only to be inherited, and mark a method abstract, with ; instead of a body, when every derived class must supply its own version. Each class that isn't abstract itself overrides every abstract method it inherits, with override and the same signature. The abstract class can still hold ordinary methods, such as Describe, that call those methods for every shape.

Additional Resources​