You have Employee
instance and you assign an instance of the ContractEmployee
to it. So you can do something like this (I'll take example from Wayne Gaylard):
class Employee
{
public void Talk()
{
MessageBox.Show("I am an Employee.");
}
}
class ContractEmployee: Employee
{
public void ContractTalk()
{
MessageBox.Show("I am a Contract Employee.");
}
}
Then you can do:
static void CreateEmployee()
{
Employee e = new ContractEmployee();
e.Talk();
((ContractEmployee)e).ContractTalk();
}
But you can't do:
static void CreateEmployee()
{
Employee e = new Employee();
e.Talk();
((ContractEmployee)e).ContractTalk(); // Here you will get an exception at runtime. Compliler won't find any errors.
}
On this example it seems useless, but it is sometimes useful. For example you can have a class structure of different user type, all inheriting from User
, you can store them on in a collection of User
s
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.