Liskov’s Substitution Principle Violations in Fintech Software Development

Burhan ARAS
iOS Interview University
3 min readJul 22, 2023

Hello fintech enthusiasts and fellow developers! Welcome to this week’s blog post, where we embark on an exploration of Liskov’s Substitution Principle (LSP) in the realm of fintech software development. As we all know, the fintech industry demands robust and reliable software systems to handle sensitive financial transactions. Let’s delve into some real-world fintech-related examples to understand LSP violations and learn how to ensure a solid design for our financial applications.

LSP: Objects in your programs, should be replaceable by their subtypes without breaking any functionality.

Simplicity is prerequisite for reliability.

1. Contradictory Behavior — Transaction and Payment:

In a fintech application, we often have classes representing financial transactions and payments. Let’s consider these classes:

class Transaction {
func execute() {
// Perform the transaction logic.
}
}

class Payment: Transaction {
// Violation of LSP - Contradictory behavior
override func execute() {
// Perform payment-specific logic.
// This implementation contradicts the generic transaction logic.
}
}

n this scenario, the Payment class behaves differently from a generic Transaction. When we attempt to substitute a Payment object for a Transaction object, it may lead to unexpected behavior, as the specific payment logic would be executed instead of the intended transaction logic.

2. Strengthened Post-Conditions — Loan and Mortgage:

Fintech applications often deal with different types of loans, including mortgages. Let’s examine the following classes:

class Loan {
func calculateInterest() -> Double {
// Calculate interest based on the loan type.
return 0.0
}
}

class Mortgage: Loan {
// Violation of LSP - Strengthened post-conditions
override func calculateInterest() -> Double {
// Calculate mortgage-specific interest.
// This implementation strengthens the post-condition.
return super.calculateInterest() * 1.2
}
}

In this case, the Mortgage class strengthens the post-condition by adding 20% to the calculated interest. When used in place of a Loan, it may return a different result, which can lead to unexpected behavior in the application's financial calculations.

3. Unsupported Operations — Account and InvestmentAccount:

Fintech applications often handle various types of financial accounts, including investment accounts. Let’s take a look at the following classes:

class Account {
func withdraw(amount: Double) {
// Withdraw the specified amount from the account.
}
}

class InvestmentAccount: Account {
// Violation of LSP - Unsupported operation
override func withdraw(amount: Double) {
// Withdraw from an investment account is restricted.
// This implementation violates LSP by supporting a restricted operation.
}
}

In this example, the InvestmentAccount class violates LSP by allowing withdrawal from an investment account, which is generally restricted. It results in unsupported operations when using InvestmentAccount objects as general Account objects, potentially leading to unexpected financial transactions.

4. Redefining Inherited Properties — Wallet and MobileWallet:

Fintech applications often handle digital wallets and mobile payment systems. Let’s examine the following classes:

class Wallet {
var balance: Double = 0.0
}

class MobileWallet: Wallet {
// Violation of LSP - Redefining inherited property
var balance: Double = 0.0

override init() {
// Initialize the mobile wallet with a bonus balance.
self.balance = 50.0
}
}

Here, the MobileWallet class redefines the balance property from the parent Wallet class, setting it to a different value during initialization. This can lead to confusion when using MobileWallet objects interchangeably with general Wallet objects, resulting in unexpected financial calculations.

Conclusion:

In the dynamic and rapidly-evolving fintech industry, adhering to Liskov’s Substitution Principle is essential for building robust and flexible financial software. By ensuring that derived classes can be used interchangeably with their base classes, we can create maintainable and reliable systems. When designing fintech applications, always be vigilant for potential LSP violations, and aim for a solid and consistent object-oriented design.

Thank you for joining me on this journey to understand Liskov’s Substitution Principle in fintech software development. Happy coding, and stay tuned for more exciting topics in the world of software engineering! 🚀

--

--