C# Overriding
Replace a parent method on the child so the call uses the child's body. Keep public class Truck : Car. On Car, mark Horn as virtual and keep it printing beep. On Truck, write Horn again with override so it prints honk. Inside Pine.Main, build a Truck and call t.Horn(). Run with dotnet run. The screen should show honk. A second demo also builds a plain Car, so you see beep from the parent and honk from the child. Leave interfaces for the next page. This lesson is one method name with two bodies.
Replace Horn on the child
The inheritance page left Truck empty, so t.Horn() kept the parent beep. The base page ran constructors with : base(). This page changes the method body on the child. Open the Pine file. The starting class name stays Pine. Delete leftover constructor prints of car and truck if they bury the method story, leftover Path demos, leftover File.WriteAllText writers, and any ReadLine line. The program must not wait for typing. Keep using System; at the top.
Give the parent a public method marked virtual that prints beep:
public virtual void Horn()
Console.WriteLine("beep");
Give the child the same method name, marked override, but print honk:
public override void Horn()
Console.WriteLine("honk");
The matching name and the matching empty parameter list mean the child version replaces the parent version for Truck objects. You are not adding a second unrelated method. The word override says this method replaces the parent's Horn.
Inside Main, build one truck and call the method:
Truck t = new Truck();
t.Horn();
The call runs the body written on Truck, so the screen prints honk.
using System;
public class Car
{
public virtual void Horn()
{
Console.WriteLine("beep");
}
}
public class Truck : Car
{
public override void Horn()
{
Console.WriteLine("honk");
}
}
public class Pine
{
public static void Main()
{
Truck t = new Truck();
t.Horn();
}
}
Save, then run with dotnet run. The screen shows honk. Walk the run once. C# starts at Pine.Main. new Truck() builds a truck. t.Horn() looks at the real type of that object. Because Truck wrote its own Horn with override, that child body runs and prints honk. The parent body that prints beep stays on Car and is not used for this truck call.
If you still see beep, check whether Truck really contains a Horn method. An empty Truck from the inheritance page still uses the parent body. Replacing a method requires the child method to be present, with the same name, and marked override. If you still see car, then truck, from a constructor demo, switch to the override file and run again.
Say the rule out loud while you watch the screen: same method name on the child, child body wins for that object. Keep Truck : Car so a truck stays a kind of car. Without that colon, a Horn on Truck would belong to a separate class, not a replacement of the parent method.
Do not invent an interface on this page. Interfaces wait for the next lesson. Today one class pair carries two Horn bodies, and the child's body answers the call on a truck.
The parent still beeps on its own
A second program keeps the replacement. Main builds both a Car and a Truck, then calls Horn on each. The parent still prints beep. The child still prints honk.
using System;
public class Car
{
public virtual void Horn()
{
Console.WriteLine("beep");
}
}
public class Truck : Car
{
public override void Horn()
{
Console.WriteLine("honk");
}
}
public class Pine
{
public static void Main()
{
Car c = new Car();
Truck t = new Truck();
c.Horn();
t.Horn();
}
}
Run again with dotnet run. The screen shows beep, then honk. The first line comes from the Car object. The second line comes from the Truck object. Replacing the method on the child does not erase the parent method for parent objects. Each object uses the body that belongs to its own type.
Read the two Horn methods once more. Same name. Same empty parameter list. Different print text. That sameness of shape, plus the words virtual and override, is what makes the child version a replacement instead of a new unrelated method. If you rename the child method to something else, trucks would print beep again, and you would no longer be teaching a replacement.
If leftover constructor files with : base() still sit nearby, keep them as history. For this lesson, run the override file so the screen shows method prints, not constructor words.
📊 Who prints what for Horn
| Piece | Horn body |
|---|---|
| Car | prints beep; marked virtual |
| Truck | prints honk; marked override |
Who prints what
The table is the short map of this page. Inheritance alone would put beep on both rows when the child stays empty. After the child writes override, the Truck row changes to honk while the Car row stays beep. Keep that map in mind when the second demo prints both lines. The first column names the class. The second column names the body that runs when you call Horn on an object of that class.
The two words virtual and override
C# will not treat a child method as a replacement unless both words are present. virtual on Car says that Horn may be replaced by a child. override on Truck says that this Horn is that replacement. Java writes the same idea with no extra words, just a second method of the same name. In a C# file those two words are required, so do not copy the Java shape here.
If you write override on Truck but forget virtual on Car, the build refuses. It reports that Horn cannot be overridden because it is not marked virtual. Put virtual back on the parent method. If you write a second Horn on Truck and forget override, the build warns that the child method hides the parent method. Hiding is a different story, and it is not this lesson. Add override, and keep virtual on Car, so the warning goes away and the replacement is real.
Both methods stay public, the same way the inheritance page required public on Horn. A replacement must be at least as visible as the method it replaces. Dropping public on the child method makes the build refuse. Keep the names spelled Horn on both classes, with the same empty parentheses.
Compared with the empty child and with base
On the inheritance page, an empty Truck kept the parent Horn and printed beep. On the base page, constructors ran through : base() and printed car, then truck. On this page, the method itself changes on the child. You still need the class colon. You still may use constructors later. You do not need an interface yet.
If a leftover empty Truck is still the file you run, add the child Horn that prints honk before you judge the screen. If a leftover constructor file is still the one you run, switch to the override file so t.Horn() is the call under test. The new pieces are virtual, override, and the two different screen words beep and honk.
What this leaves for interfaces
This page replaces a method inside a class pair joined by a colon. The next page introduces interfaces, where a type can promise methods without being a child of one parent class. Stay with virtual and override here. Do not add an interface as a requirement on these Pine demos yet.
When you later meet an interface, the word override can appear again, because a class fills in a promised method. Treat this page as the class-pair half of that story. Run both Pine demos until honk from a truck and beep from a car feel automatic before you open the interfaces lesson.
Before interfaces
You can write override Horn on Truck and see honk from t.Horn(). You can call both c.Horn() and t.Horn() and see beep, then honk. Run Pine with dotnet run for each kept demo in turn. The child body answered the truck call. The parent body still answered the car call. The next page is interfaces.
Keep the single-truck honk demo for a clear replacement. Keep the two-object demo for a clear parent-versus-child contrast. Keep interface files off this page. Those three habits are the checkpoint before you move on. When honk appears after t.Horn() and beep still appears after c.Horn(), overriding did its job for this lesson, and you are ready for the next topic about interfaces.
One last pass through the file: one Pine with Main, virtual on Car, override on Truck, a child body that prints honk, no keyboard wait, and no forced interface. Run dotnet run once more on the demo you keep open. If the screen still matches the table, parent beep and child honk, the replacement 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