Bird
Raised Fist0

How do interfaces facilitate writing a method that processes different payment types like CreditCard and PayPal?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Interfaces
How do interfaces facilitate writing a method that processes different payment types like CreditCard and PayPal?
ABy forcing all payment classes to inherit from a single base class
BBy defining a common interface with payment methods that each payment type implements
CBy using abstract classes instead of interfaces
DBy writing separate methods for each payment type
Step-by-Step Solution
Solution:
  1. Step 1: Common interface for payments

    Define an interface like IPayment with methods such as ProcessPayment().
  2. Step 2: Implement interface in payment classes

    CreditCard and PayPal classes implement IPayment with their own logic.
  3. Step 3: Write method accepting interface type

    The method can accept IPayment parameter and work with any payment type.
  4. Final Answer:

    By defining a common interface with payment methods that each payment type implements -> Option B
  5. Quick Check:

    Interfaces enable polymorphic method parameters [OK]
Quick Trick: Use interfaces to unify different payment types [OK]
Common Mistakes:
MISTAKES
  • Using inheritance instead of interfaces
  • Writing duplicate methods for each payment type
  • Confusing abstract classes with interfaces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes