C# Inheritance & Casting

You can cast a subtype to its base type. But you are casting an instance of the base type to the subtype.

An EmployeeProfile is-an Employee. Not necessarily the other way around.

So this would work:

EmployeeProfile prof = new EmployeeProfile();
Employee emp = prof;

However, this model reeks of bad design. An employee profile is not a special kind of an employee, is it? It makes more sense for an employee to have a profile. You are after the composition pattern here.


All the answers are correct...just providing a no frills simple explanation...

class Employee

class Female : Employee

class Male: Employee

Just because you are an Employee does not make you a Female...

Tags:

C#

Inheritance