C# Polymorphism
Store different objects in a parent-typed variable so one call name can run different bodies. Keep Horn on Machine as virtual, printing tone. On Truck, override Horn to print honk. On Car, override Horn to print beep. Inside Pine.Main, assign Machine m = new Machine();, Machine t = new Truck();, and Machine c = new Car();, then call Horn on each. Run with dotnet run. The screen should show tone, then honk, then beep. A second demo keeps only the two children in Machine variables. Leave equals for the next page. This lesson is one parent type holding many forms.
One parent type, three Horn bodies
The abstract page left Horn unfinished, so you could not write new Machine(). This page gives Machine a real Horn body and then calls that name through parent-typed variables. Open the Pine file. The starting class name stays Pine. Delete leftover abstract methods and Park prints if they bury this story, leftover enum Level files, leftover Path demos, and any ReadLine line. The program must not wait for typing. Keep using System; at the top.
Declare a normal parent. The method has a body, and it is marked virtual so a child may replace it:
public virtual void Horn()
Console.WriteLine("tone");
Declare the children with a colon. Each writes the same method with override and a different print:
public class Truck : Machine
public override void Horn()
Console.WriteLine("honk");
Car does the same and prints beep. The matching name and the empty parameter list, plus the words virtual and override, mean each child replaces the parent method.
Inside Main, build three variables whose declared type is Machine:
Machine m = new Machine();
Machine t = new Truck();
Machine c = new Car();
Then call:
m.Horn();
t.Horn();
c.Horn();
The declared type is Machine for all three. The real object decides which Horn body runs.
using System;
public class Machine
{
public virtual void Horn()
{
Console.WriteLine("tone");
}
}
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()
{
Machine m = new Machine();
Machine t = new Truck();
Machine c = new Car();
m.Horn();
t.Horn();
c.Horn();
}
}
Save, then run with dotnet run. The screen shows tone, then honk, then beep. Walk the run once. C# starts at Pine.Main. m holds a plain Machine, so m.Horn() prints tone. t holds a Truck even though the variable type says Machine, so t.Horn() prints honk. c holds a Car, so c.Horn() prints beep. Same call spelling. Three bodies.
Point at each assignment while you read the screen line that matches it. The first line belongs to the plain machine. The second line belongs to the truck object behind a machine-typed name. The third line belongs to the car object behind a machine-typed name.
The words virtual and override are what make the second and third lines use the child bodies. If you delete virtual from Machine and drop override on the children, a Machine variable runs the parent body even when the object is a Truck. The truck line would print tone, and the build would warn that the child method only hides the parent method. Hiding is not this lesson. Put virtual back on the parent and override back on each child so the real object chooses the body.
If you still see honk then park from the abstract demo, move to the polymorphism file and run again. If every variable is typed Truck or Car only, you are not yet showing the parent-typed form this page teaches. Keep Machine on the left of the assignments. The other direction does not build: you cannot store a plain Machine in a Truck variable, because not every machine is a truck.
Say the rule out loud while you watch the screen: the variable type can be the parent, the object can be the child, and the call runs the child's body when the child overrode a virtual method. Do not invent an equals method on this page. That topic waits for the next lesson.
Two children through Machine variables
A second program keeps the overrides. Main builds only Truck and Car, still stored as Machine, and calls Horn on each.
using System;
public class Machine
{
public virtual void Horn()
{
Console.WriteLine("tone");
}
}
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()
{
Machine t = new Truck();
Machine c = new Car();
t.Horn();
c.Horn();
}
}
Run again with dotnet run. The screen shows honk, then beep. There is no plain Machine object in this second demo, yet both variables are still typed Machine. The child objects supply the bodies. That is the same idea with a shorter screen. The parent body that prints tone is still in the file. Nothing in Main calls it this time.
Read the assignment lines once more. The left side is Machine. The right side is new Truck() or new Car(). The left side names the type you call through. The right side names the real object. Keep Truck : Machine and Car : Machine so those assignments stay legal. Keep virtual on the parent and override on both children. Without that pair, both lines would print tone.
If leftover abstract demos still sit nearby, keep them as history. For this lesson, run the polymorphism file so the screen shows tone, honk, and beep, or honk and beep, not park as the main story.
📊 Who runs for each Machine variable
| Variable | Real object | Horn prints |
|---|---|---|
| m | Machine | tone |
| t | Truck | honk |
| c | Car | beep |
Which Horn body runs
The table is the short map of the first demo. The variable type stays Machine on every row. The real object picks the print. After override, the child rows differ from the parent row. Keep that map in mind when the second demo drops the parent object and keeps only the t and c rows. The first column is the variable name in Main. The middle column is the object new built. The last column is the word on the screen.
When the child body does not run
Four small mistakes put tone on a line that should say honk. Dropping virtual from Machine makes every Machine variable use the parent body. Dropping override on Truck hides the parent method instead of replacing it, and the truck line prints tone again. Typing the variables as Truck and Car only never shows a parent-typed call. Writing Truck t = new Machine() asks a truck variable to hold a plain machine, and the build refuses. Fix the first two by restoring virtual and override. Fix the third by writing Machine on the left. Fix the fourth by building the child on the right: Machine t = new Truck();.
Compared with the abstract page
On the abstract page, Machine left Horn unfinished, children filled it with override, and a shared Park printed park. You could not write new Machine(). On this page, Machine already has a tone body, the method is virtual, children override it, and parent-typed variables hold the objects. You can write new Machine() again. You still run with dotnet run. The new pieces are assignments such as Machine t = new Truck(); and three different prints from one call name.
If a leftover abstract file is still the one you run, you will see abstract void Horn(); and Park. Move to the polymorphism file so Machine has a normal virtual Horn body and Main assigns Machine variables to different objects. Keep equals demos off this page. Keep keyboard input off this page.
Before equals
You can assign Machine variables to Machine, Truck, and Car objects and see tone, then honk, then beep. You can keep only the two children in Machine variables and see honk, then beep. Run Pine with dotnet run for each kept demo in turn. The call used the body that belongs to the real object. The next page is equals, where two objects are compared for sameness in a small teaching demo.
Keep the three-object demo for a clear parent-plus-children screen. Keep the two-child demo for a clear parent-typed pair. Keep equals files off this page. Those three habits are the checkpoint before you move on. When honk appears from a Machine-typed truck variable, polymorphism did its job for this lesson, and you are ready for the next topic about equals.
One last pass through the file: one Pine with Main, virtual on Machine, override on Truck and Car, parent-typed variables, no Park requirement, and no keyboard wait. Run dotnet run once more on the demo you keep open. If the screen still matches the table, the many-forms call 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