CodeBlanks Logo Icon CodeBlanks

Summary - Interface

Combine interface and default methods to write a payment system.

Summary: Payment system

In this summary you'll design a payment system. The PaymentSystem interface defines the pay method contract and a default method that prints a receipt. The CreditCard class implements this contract.

First you'll define the PaymentSystem interface: a void pay(double amount) signature and a default void receipt(double amount) body. receipt prints the amount. Then the CreditCard class writes pay via implements PaymentSystem. This structure shows how the interface and default method work together in a real scenario.

Practice Define the PaymentSystem interface

Define an interface PaymentSystem. Add the void pay(double amount) signature. Add a default void receipt(double amount) method; its body runs System.out.println("Receipt: " + amount);. Do not define an interface variable in main; only write the interface. Then in class Main with a main method run System.out.println("Ready");.

Starter code

Main.java
// Define the PaymentSystem interface
// Add pay signature and default receipt method
// In Main print a ready message

Sample solution

Main.java
interface PaymentSystem {
    void pay(double amount);
    default void receipt(double amount) {
        System.out.println("Receipt: " + amount);
    }
}
class Main {
    public static void main(String[] args) {
        System.out.println("Ready");
    }
}

Expected output

Ready

Practice Implement the CreditCard class

Write both the PaymentSystem interface and the CreditCard class in this file. The interface has a void pay(double amount) signature and a default void receipt(double amount) method; the receipt body runs System.out.println("Receipt: " + amount);. Write class CreditCard implements PaymentSystem; write the public void pay(double amount) method with a body that runs System.out.println("Paid by credit card: " + amount);. In main create CreditCard card = new CreditCard(); then call card.pay(150) and card.receipt(150).

Starter code

Main.java
// Define the PaymentSystem interface
// Write CreditCard implements PaymentSystem
// Write the pay method with a body
// Call pay and receipt on the card in main

Sample solution

Main.java
interface PaymentSystem {
    void pay(double amount);
    default void receipt(double amount) {
        System.out.println("Receipt: " + amount);
    }
}
class CreditCard implements PaymentSystem {
    public void pay(double amount) {
        System.out.println("Paid by credit card: " + amount);
    }
}
class Main {
    public static void main(String[] args) {
        CreditCard card = new CreditCard();
        card.pay(150);
        card.receipt(150);
    }
}

Expected output

Paid by credit card: 150
Receipt: 150

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How should the receipt method be in the PaymentSystem interface?

  • abstract method
  • default method Correct answer
  • private method
  • static method

Explanation: receipt is a default method with a body.

2 Match the payment system parts

Term Meaning
PaymentSystem Contract
CreditCard Implementing class
pay Abstract method
receipt default method

3 Complete the implements line

Question
class CreditCard ____ PaymentSystem {
    public void pay(double amount) { }
}
Answer
class CreditCard implements PaymentSystem {
    public void pay(double amount) { }
}

Explanation: CreditCard implements PaymentSystem.

All lessons in Intermediate