C# Abstract Classes
Mark a parent class abstract when it must leave at least one method without a body for a child to finish. Write public abstract class Machine with public abstract void Horn(); and a normal method Park that prints park. Write public class Truck : Machine and give Horn a body with override that prints honk. Inside Pine.Main, build a Truck, call t.Horn(), then call t.Park(). Run with dotnet run. The screen should show honk, then park. A second demo adds Car so two children each fill Horn with a different word. Leave polymorphism for the next page. This lesson is the unfinished parent method and the child that finishes it.
Fill the abstract method on the child
The enums page listed fixed names such as Level.Medium. This page leaves a method unfinished on purpose. Open the Pine file. The starting class name stays Pine. Delete leftover enum Level demos if they bury the abstract story, leftover Signal and Mark files, leftover Path demos, and any ReadLine line. The program must not wait for typing. Keep using System; at the top.
Declare the abstract parent above the other types:
public abstract class Machine
Inside it, name the unfinished method. The line ends with a semicolon. There are no braces and no print:
public abstract void Horn();
The word abstract appears twice: once on the class, once on the method. The method must also say public. An abstract method cannot be private, because a child in another class has to reach it.
Then write a normal method that already has a body, so every child can reuse it:
public void Park()
Console.WriteLine("park");
Declare the child with a colon. This colon names a parent class, the same shape as Truck : Car on the inheritance page:
public class Truck : Machine
Inside Truck, fill Horn. The child method must say override:
public override void Horn()
Console.WriteLine("honk");
You must fill every abstract method listed on the parent. On this page that is only Horn. Do not write new Machine() in Main. An abstract class is not built as an object on its own.
Inside Main:
Truck t = new Truck();
t.Horn();
t.Park();
The first call runs the body on Truck. The second call runs the shared body on Machine.
using System;
public abstract class Machine
{
public abstract void Horn();
public void Park()
{
Console.WriteLine("park");
}
}
public class Truck : Machine
{
public override void Horn()
{
Console.WriteLine("honk");
}
}
public class Pine
{
public static void Main()
{
Truck t = new Truck();
t.Horn();
t.Park();
}
}
Save, then run with dotnet run. The screen shows honk, then park. Walk the run once. C# starts at Pine.Main. new Truck() builds a truck that finished the abstract Horn. t.Horn() prints honk. t.Park() prints park from the parent body that already existed. The unfinished method lived only as a requirement until the child wrote the body.
If the build refuses, check four things. The parent class line must say abstract class Machine. The unfinished method must say abstract, say public, and end with a semicolon. Truck must write Horn with a body. That child method must say override. If you still see Medium from an enum file, move to the abstract file and run again. If you try new Machine(), the build refuses, and that refusal is the point of an abstract class.
Say the rule out loud while you watch the screen: the abstract parent names the missing method, the child fills it with override, and shared methods still run. Do not build a row of parent-typed variables that point at many children yet. That story waits for the next lesson.
Two children fill Horn differently
A second program keeps the same abstract Machine. Truck still prints honk. A new Car prints beep. Main builds both, calls each Horn, then calls Park on the truck.
using System;
public abstract class Machine
{
public abstract void Horn();
public void Park()
{
Console.WriteLine("park");
}
}
public class Truck : Machine
{
public override void Horn()
{
Console.WriteLine("honk");
}
}
public class Car : Machine
{
public override void Horn()
{
Console.WriteLine("beep");
}
}
public class Pine
{
public static void Main()
{
Truck t = new Truck();
Car c = new Car();
t.Horn();
c.Horn();
t.Park();
}
}
Run again with dotnet run. The screen shows honk, then beep, then park. Each child filled the same abstract name with its own print. The shared Park body still comes from Machine. You still never write new Machine().
Read the parent once more. One abstract method. One normal method. Read the children. Each writes Horn with override. Neither rewrites Park on this page. That split is the teaching point. If you omit Horn on Car while Car : Machine, the build refuses, because every abstract method must be filled or the child class itself must be marked abstract. This page keeps both children concrete, so both must override Horn.
If leftover enum demos still sit nearby, keep them as history. For this lesson, run the abstract file so the screen shows method fills, not Medium.
Why this override is not the interface page
On the overriding page, Horn on Car already had a body and was marked virtual. The child override replaced that body. On the interfaces page, void Horn(); lived on Signal, and Truck : Signal filled it with public void Horn() and no override word. On this page the unfinished method lives on a class, it is marked abstract, and the child must say override.
If you copy the interface shape and write public void Horn() on Truck without override, the build still says Truck does not implement the abstract member. The method you wrote is only hiding a name. It does not finish the abstract requirement. Put override back on the child method.
If you mark the child method override but forget abstract on the parent method, the build says there is nothing to override, the same complaint the overriding page met when virtual was missing. An abstract method is the unfinished form of a method a child may override. A normal method with a body needs virtual before a child may override it. Keep those two parent words apart: abstract here, virtual when a body already exists.
Compared with enums and interfaces
On the enums page, Level listed the names Low, Medium, and High. On the interfaces page, a colon listed Signal and the class filled a promised method. On this page, abstract class plus a colon leaves a class method unfinished until a child writes override. You still run with dotnet run. The new pieces are abstract on the class, abstract on the method, override on the child, and a shared Park that nobody rewrites.
If a leftover enum Level file is still the one you run, you will see constants and maybe a switch. Move to the abstract file so the top shows public abstract class Machine and Main builds a Truck. Keep keyboard input off this page.
What this leaves for polymorphism
This page finishes abstract methods on concrete children and calls those methods on variables whose types are Truck and Car. The next page shows polymorphism, where one parent-typed variable can hold different child objects and the call still reaches the child body. Stay with abstract parents and filled children here. Do not require a list of Machine variables that point at many children yet.
When you later write polymorphism demos, you will still often start from an abstract method name. Treat this page as the unfinished-method half of that story. Run both Pine demos until honk then park, and then honk, beep, park, feel automatic before you open the polymorphism lesson.
Before polymorphism
You can declare public abstract class Machine, override Horn on Truck, and see honk then park. You can add Car with its own override and see honk, then beep, then park. Run Pine with dotnet run for each kept demo in turn. The child finished the abstract method. The next page is polymorphism, where a call through a parent-typed variable picks the child body.
Keep the single-truck demo for a clear abstract fill plus shared Park. Keep the two-child demo for a clear pair of fills. Keep a row of Machine variables off this page. Those three habits are the checkpoint before you move on. When honk appears before park after new Truck(), abstract classes did their job for this lesson, and you are ready for the next topic about polymorphism.
One last pass through the file: one Pine with Main, one abstract Machine, concrete children that override Horn, a shared Park, no new Machine(), and no keyboard wait. Run dotnet run once more on the demo you keep open. If the screen still prints the fills you wrote, the abstract requirement is solid.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top