C# Inheritance
Make a child class with a colon so it can use a method written on the parent. Put a Horn method on Car so it prints beep. Write public class Truck : Car with an empty body. Inside Pine.Main, build a Truck and call t.Horn(). Run with dotnet run. The screen should show beep. A second demo also builds a plain Car, so you see the parent still works after the child exists. Leave base for the next page. Do not write a second Horn inside Truck on this page, because overriding comes later in the course.
A child keeps a parent method
The paths page named places with Path.Combine and Path.GetFileName. This page starts a new module: one class can be a child of another. Open the Pine file. The starting class name stays Pine. Delete leftover Path demos, leftover File.WriteAllText writers, leftover File.ReadAllLines readers, and any ReadLine line. The program must not wait for typing. Keep using System; at the top. This lesson needs no System.IO line.
Above Pine, declare the parent class:
public class Car
Inside Car, write a public instance method named Horn that prints beep:
Console.WriteLine("beep");
Then declare the child class with a colon after its name:
public class Truck : Car
Keep the Truck body empty on this page. The child does not rewrite Horn. Empty braces are enough to say that a Truck is a kind of Car.
Inside Main, build a truck and call the inherited method:
Truck t = new Truck();
t.Horn();
The call works because : Car connects Truck to Car. You did not copy Horn into Truck.
using System;
public class Car
{
public void Horn()
{
Console.WriteLine("beep");
}
}
public class Truck : Car
{
}
public class Pine
{
public static void Main()
{
Truck t = new Truck();
t.Horn();
}
}
Save, then run with dotnet run. The screen shows beep. Walk the run once. C# starts at Pine.Main. new Truck() builds a truck object. t.Horn() looks for Horn on Truck, finds nothing written there, and then finds it on the parent Car through inheritance. The method prints beep. Nothing else is printed, and no file is opened.
If the build says that Truck does not contain a definition for Horn, the : Car part is missing or misspelled. Put it back right after the child name. If the build says Horn is inaccessible because of its protection level, the word public is missing in front of void Horn(). A method without an access word is private to Car, and the access page already showed that Pine cannot reach a private member. If the screen shows note.txt or docs, a paths leftover is still in Main. Replace it with the truck demo.
Read the colon carefully. The child name comes first and the parent name comes after the colon: Truck : Car, not the reverse order. Java writes the same idea with the word extends, but C# uses only the colon here. Do not type extends in a C# file; the compiler does not know that word.
Do not write base on this page. Calling into the parent on purpose with base is the next lesson. Do not write a second Horn inside Truck either, because that story about replacing a parent method comes later. Today the child stays empty and still uses the parent method. That empty body is intentional teaching, not a mistake to fix by copying Horn down into Truck.
📊 Parent and child on this page
| Piece | Role |
|---|---|
| Car | parent class that owns Horn |
| Truck | child class through : Car |
| Pine | starting class with Main |
| Horn | prints beep; written only on Car |
The parent still works on its own
A second program keeps the same Car, the same empty Truck : Car, and the same Pine. Inside Main, build both a Car and a Truck, and call Horn on each one.
using System;
public class Car
{
public void Horn()
{
Console.WriteLine("beep");
}
}
public class Truck : Car
{
}
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 beep again. The first line comes from the plain Car. The second line comes from the Truck that inherited Horn. Making a child did not delete or break the parent class. Both objects call the same method body, written once on Car. You still build each object yourself with new; the colon line does not create a truck for you automatically.
That contrast is the checkpoint. Inheritance adds a child path to an existing method. It does not require you to remove the parent. If you remove : Car from Truck, then t.Horn() stops building, because Truck no longer receives Horn. Keep the colon on the teaching child. Prefer the single-truck file when you want one quiet beep. Prefer the two-object file when you want to prove the parent still answers.
A Truck is also a Car, so the child can stand where the parent is expected. That idea grows on later pages. For now, notice only that one method written in one place serves two kinds of objects.
Compared with paths
On the paths page, dotnet run printed note.txt and docs from Path helpers that work on text. On this page, dotnet run connects classes with a colon. Keep the paths file only as history if it still sits nearby, and run the inheritance demos so the screen matches this lesson.
The colon after a class name, parent method reuse, and an empty child body are the new pieces. Classes, new, public, and instance methods came from earlier object pages. Today those pieces meet inheritance for the first time in this course. Keep the teaching names Car, Truck, and Pine so the code stays short and the output stays easy to check.
Mistakes to skip on this page
Writing Car : Truck turns the story around, so Car would try to be a child of Truck. Writing extends copies Java and does not build. Leaving out public on Horn hides the method from Pine. Copying Horn into Truck hides the lesson, because the point is that the child did not write it. Adding base before the next page invents a requirement that this demo does not need. Each mistake is small, and each one has a clear build message or a wrong screen that tells you where to look.
What this leaves for base
This page shows a child that uses a parent method without writing base. The next page introduces base, so a child can call into the parent on purpose, especially around constructors. Stay with the colon and the inherited Horn call here. Do not add base as a requirement on these Pine demos yet.
Before base
You can write public class Truck : Car, build a Truck, and see beep from Horn. You can also build a plain Car and see the parent still print beep. Run Pine with dotnet run for each kept demo in turn. The child stayed empty and still used the parent method. The next page is base, where the child talks to the parent more directly.
Keep the single-truck demo for a clear inherited call. Keep the two-object demo to prove the parent still works on its own. Keep base off this page for now. Those three habits are the checkpoint before you move on. When beep appears from a Truck that never wrote its own Horn, inheritance did its job for this lesson, and you are ready for the next topic about base.
🧠 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