C# base
Use base on a child constructor so the parent constructor runs first. Keep public class Truck : Car. Give Car a constructor that prints car. Give Truck a constructor written as public Truck() : base() whose body prints truck. Inside Pine.Main, build one truck. Run with dotnet run. The screen should show car, then truck. A second demo passes a mark with base("car") into Car(string mark). Leave overriding a method for the next page, because this lesson is only about constructor order with base.
Call the parent constructor first
The inheritance page let an empty Truck use Horn from Car. This page adds constructors to both classes. Open the Pine file. The starting class name stays Pine. Delete leftover Horn calls if they crowd the screen, 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 constructor with no parameters that prints car:
public Car()
Console.WriteLine("car");
Give the child a public constructor. Put : base() after the constructor name and its parentheses, before the opening brace. Inside the body, print truck:
public Truck() : base()
Console.WriteLine("truck");
Inside Main, build one truck:
Truck t = new Truck();
Building the truck calls Truck(). Before the body of Truck() runs, the : base() part calls Car(). After the parent constructor finishes, the child body runs.
using System;
public class Car
{
public Car()
{
Console.WriteLine("car");
}
}
public class Truck : Car
{
public Truck() : base()
{
Console.WriteLine("truck");
}
}
public class Pine
{
public static void Main()
{
Truck t = new Truck();
}
}
Save, then run with dotnet run. The screen shows car, then truck. Walk the run once. C# starts at Pine.Main. new Truck() asks for the Truck() constructor. Its header says : base(), so Car() runs first and prints car. Then control comes back to the body of Truck(), which prints truck. The parent line appears before the child line because the parent constructor always finishes before the child body starts.
Say the order out loud while you watch the screen: parent constructor, then child body. If the lines arrive in another order, check that Console.WriteLine("truck"); sits inside the braces of Truck() and that Console.WriteLine("car"); sits inside Car(), not the other way around.
The place of base matters. In C#, : base() belongs to the constructor header, outside the braces. Writing base(); as a line inside the body, the way Java writes super();, does not build. If the build points at a line that starts with base inside the braces, move it up after public Truck() with a colon in front.
On this first demo you could even remove : base(), and the screen would still show car, then truck. When a parent has a constructor with no parameters, C# calls it for you before the child body. Writing : base() makes that hidden step visible, which is why the teaching file keeps it. Keep both constructors public. A constructor without an access word is private, and then new Truck() in Pine cannot reach it. If you see only beep from an old inheritance demo, a Horn leftover is still the program you run. Switch to the constructor file and run again.
Pass a value into the parent with base
A second program keeps Truck : Car. The parent constructor now takes a string mark and prints that mark. The child constructor writes : base("car") in its header and then prints truck in its body.
using System;
public class Car
{
public Car(string mark)
{
Console.WriteLine(mark);
}
}
public class Truck : Car
{
public Truck() : base("car")
{
Console.WriteLine("truck");
}
}
public class Pine
{
public static void Main()
{
Truck t = new Truck();
}
}
Run again with dotnet run. The screen again shows car, then truck. This time car is the value you passed through base("car"), not a fixed print inside a parameterless Car(). The child chose the text; the parent constructor printed it. After that, the child body printed truck.
Read the parent constructor once more: Car(string mark). The text in quotes inside base("car") becomes mark. Change nothing else for this check. Keep Truck : Car, keep the child print of truck, and keep new Truck() in Main. The screen still ends with truck under car, because the child body runs only after the parent constructor returns.
Now the hidden call from the first demo is gone. Car has no constructor without parameters, so C# cannot call one for you. If you delete : base("car"), the build refuses and reports that no argument was given for the required parameter mark of Car(string mark). Writing the matching base(...) is how the child starts the parent with the value it needs. When the build reports that missing constructor, look at the child constructor header first and confirm that : base("car") is still there.
Compared with the inheritance page
On the inheritance page, an empty Truck called Horn without any constructor. On this page, constructors run in order through base. Keep the Horn demos as history if they still sit nearby, and run the base file with dotnet run so the screen shows car, then truck.
The inheritance page proved that the colon on a class shares a method. This page proves that the same colon also shares a building path: the child constructor starts the parent through base, then continues with its own body. Both lessons keep Truck : Car. Notice that C# uses a colon twice now, with two meanings. On the class line, Truck : Car names the parent class. On the constructor line, Truck() : base() names the parent constructor to run first. Only the body of Truck changed, from empty braces to a constructor.
: base(), : base("car"), and constructor order are the new pieces. You still need the class colon. You do not need to replace a method yet. If a leftover t.Horn(); still sits in Main, remove it for these demos so the screen shows only the constructor prints.
What this leaves for overriding
This page runs parent constructors with base. The next page shows overriding, where a child writes its own version of a method the parent already has. Stay with constructor base calls here. Do not write a new Horn inside Truck as a requirement on these demos yet.
When you later replace a method, the word base will come back, because a child can also call the parent version of a method on purpose. Treat this page as the constructor half of that story. Run both Pine demos until car, then truck, feels automatic before you open the overriding lesson.
Before overriding
You can write public Truck() : base(), print truck in the body, and see car first. You can write : base("car") when the parent takes a mark and still see car, then truck. Run Pine with dotnet run for each kept demo in turn. The parent constructor finished before the child body. The next page is overriding, where the child replaces a method body.
Keep the : base() demo for a clear order. Keep the : base("car") demo for a clear value pass. Keep method replacement off this page. Those three habits are the checkpoint before you move on. When car appears above truck after new Truck(), base did its job for this lesson, and you are ready for the next topic about overriding.
One last pass through the file: one Pine with Main, two helper classes, public constructors, no keyboard wait, no path leftovers, and no forced Horn rewrite. Run dotnet run once more on the demo you keep open. If the screen still prints car, then truck, the constructor order 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