riki
riki

Reputation: 2403

Covariant rule is important in LSP? And how does it impacts the client?

I have been going through LSP with C# and I have a few confusion. Let just say

public class Employee
{
    //properties
}
    
public class EmployeeExtendedDetail : Employee
{
    //properties
}
    
public class EmployeeRepository
{
    public virtual Employee GetEmployeeDetialById(int id)
    {
        return new Employee();
    }
}

public class EmployeeExtendedDetailRepository : EmployeeRepository
{
    //Changed the return type to a more derived type
    public override EmployeeExtendedDetail GetEmployeeDetialById(int id)
    {
        return new EmployeeExtendedDetail();
    }
}

And in the client:

EmployeeRepository empRepo1 = new EmployeeExtendedDetailRepository();
Employee employee1 = empRepo1.GetEmployeeDetialById(1)

Well what I do not understand in:

LSP: There must be covariance of the return types in the subtype.

First of all should this return type has to be a derived type of Employee? and Employee just fits fine? (probably a stupid question)

Again we cannot change the return type of the method GetEmployeeDetialById to something entirely else which is not a type of Employee.

So what I an not able to see in this rule?

Upvotes: 0

Views: 44

Answers (0)

Related Questions