Grokking SOLID Design Principles
Ask Author
Back to course home

0% completed

Vote For New Content
Keshav Garg
Why can't we just extend the Invoice class for International Invoice? Why make it so complicated? What's the issue with this ??

Keshav Garg

Oct 27, 2024

public class Invoice { private double amount; public Invoice(double amount) { this.amount = amount; } public double getAmount() { return amount; } // This method handles generating basic invoices public void generateInvoice() { System.out.println("Generating basic invoice for amount: " + amount); } // This method handles applying discounts on the invoice public void applyDiscount() { System.out.println("Applying discount on invoice: " + amount); } } public class InternationalInvoice extends Invoice { @Override public void generateInvoice() { System.out.println("Generating International invoice for amount: " + amount); } }

2

0

Comments
Comments
Keshav Garg
Keshav Garga year ago

Or, we can abstract the generate Invoice method and implement it separately for basic invoice, and international invoice classes.

Jackson Tucker
Jackson Tuckera year ago

Yes, this approach also occurred to me. It seems more intuitive and still follows the principle. @designgurus can you offer an explanation for why the approach in the text is superior?

Design Gurus
Design Gurus7 months ago

Good question. Yes, you can extend the Invoice class like that, and for small projects, it might work just fine. But here’s why we avoid it in bigger or growing systems:

  • Tight coupling: If the base Invoice class changes, your InternationalInvoice might b...

On this page