I have searched for this question but could not find a good answer for my case.
Suppose I have a SuperClass for a bank loan with properties Amount
and InterestRate
.
public class BankLoan{
public double Amount {get; set;}
public double InterestRate {get; set;}
}
Then I create a derived class called SmallLoan, which will have a variable InterestRate
based on its amount.
public class SmallLoan{
public SmallLoan(){
InterestRate = CalculateInterest();
}
private double CalculateInterest(){
if(Amount < 100)
return 0.10;
else
return 0.05;
}
}
Would this be violating the LSP? I would think that it doesn't, because even if an instance of SmallLoan
tries to call CalculateInterest
outside of the class definition, it's going to throw an error, not related to the substitution but simply because it has a different access modifier. Even if only BankLoan
existed and it had the same private method, a call to it from the outside would also result in an error.
But I can see how some people would say that the presence of the error in general is a violation of the principle.
Lastly: I am aware that these classes might not be the best and brightest implementations of inheritance or even just basic class design, but the question isn't about that, I merely want to know if private methods are a violation of LSP, and I chose to illustrate it with this very simple example.
SmallLoan
inherit fromBankLoan
?