C# inheritance with base, virtual, override, sealed, and hiding with new.
In one line
Write : ParentClass after the class name to take over the parent’s properties and methods, then replace only the methods the parent opened with virtual, using override.
Why you need it
Separate classes for staff, managers and interns would each carry a name, a base salary and a pay calculation. Change the pay rule and you edit three places; miss one and people get paid by different rules.
Keep the shared part once in Employee and use override only for what really differs, like the team allowance for managers or the meal allowance for interns.
Everyday picture
Think of a company handbook. Everyone follows the same book, and the manager supplement lists only the changes, such as "add an allowance per team member". The supplement may only change chapters the handbook marks as changeable (virtual).
C# code does not run on this page. Run your edited code on your own computer with dotnet run.
When to use it
"A manager is an employee" is always true
Several classes share properties and behavior and differ in only a few spots
You want to handle many children through one parent type (Employee[]). That is the next topic, polymorphism.
A C# class has one parent class. To promise several things, add several interfaces (abstraction topic).
Hiding with new is an emergency exit for name clashes with a parent you cannot change. Use virtual and override day to day.
Common mistakes
Leaving out virtual and override
Without virtual on the parent method, a same-named child method hides it instead of overriding it. The compiler only warns (CS0108), and calls through a parent-typed variable get the parent result. Pair virtual in the parent with override in the child.
override only works on methods the parent opened with virtual (or abstract). If you own the parent, mark it virtual; otherwise override a method that allows it.
What the C# compiler sayserror CS0506: 'Lead.Badge()': cannot override inherited member 'Employee.Badge()' because it is not marked virtual, abstract, or override
A Manager is stored in an Employee variable. What is printed?
Employee e = newManager("Ara", 2_000_000m, 1);Console.WriteLine($"{e.MonthlyPay():N0} {e.Badge()}");
Show the answer
Answer: 2. 2,200,000 staffMonthlyPay is an override, so it follows the object (Manager): 2,000,000 + 200,000. Badge is only hidden with new, so it follows the variable type (Employee): staff.
Question 2
In which order do the constructors run for new Manager(...)?
Show the answer
Answer: 3. The parent constructor runs first: base(...) runs the parent constructor first to fill the inherited parts (name, base salary), then the child body fills its own part (team size). Steps 2 and 3 of the step-through show this order.
Questions people ask
What is the difference between override and new?
override is chosen by the object’s real type, new by the variable’s type. Through a parent-typed variable, an override runs the child version while a new method runs the parent version.
Why can’t C# methods be overridden by default?
Only methods the parent opens with virtual can change, so the author of the parent decides what may vary. Java takes the opposite default.
When should I use sealed?
On classes whose rules would break if someone inherited from them. You can also write sealed override on a single method to stop further overriding of just that method.