Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
A finished payment system to read: a PaymentMethod interface with a default payWithFee, an abstract BasePayment that writes pay() once, Card, Upi and Wallet rules, a Status enum, and a checkout where the Wallet with 50 declines 80 and the Card pays.
What you will learn
- Read a small system built from an interface, an abstract base class and an enum
- Use a default interface method that every implementing class gets for free
- Explain why two orders sharing one Wallet reference share one balance
- Trace a checkout that tries payment methods in order and stops at the first PAID
Before you start
- Java interfaces
- Java abstract classes
- Java enums with fields, methods and switch
This lab is a finished payment system to read and run. It puts several OOP ideas to work together: an interface sets the contract, an abstract class shares the common work, three classes each add one rule, and an enum names the result.
The contract and the shared work
package org.studyeasy.s03l18;
public interface PaymentMethod {
String name();
Status pay(double total);
default Status payWithFee(double total) {
return pay(total + 2);
}
}
name and pay have no bodies, so every payment method must provide them. payWithFee is a default method: it has a body, adds 2 to the total and calls pay, and every implementing class gets it for free.
package org.studyeasy.s03l18;
public abstract class BasePayment implements PaymentMethod {
private final String name;
protected BasePayment(String name) {
this.name = name;
}
public String name() {
return name;
}
protected abstract boolean approve(double total);
public Status pay(double total) {
Status result = approve(total) ? Status.PAID : Status.DECLINED;
System.out.println(name + " " + total + " -> " + result);
return result;
}
}
pay is written once: it asks approve, turns true into PAID and false into DECLINED, prints one line and returns the status. approve is abstract on purpose (like monthlyPay), because each payment method decides for itself.
package org.studyeasy.s03l18;
public class Card extends BasePayment {
private static final double LIMIT = 500;
public Card() {
super("Card");
}
protected boolean approve(double total) {
return total <= LIMIT;
}
}
That rule is all Card writes. Upi's approve returns true for everything.
One call, three behaviours
PaymentMethod[] methods = {
new Card(), new Upi(), new Wallet(100)
};
for (PaymentMethod m : methods) {
m.pay(80);
}
Card 80.0 -> PAID
UPI 80.0 -> PAID
Wallet 80.0 -> PAID
The loop only knows PaymentMethod. Each object answers through its own approve, and 80 is within every limit.
The default method adds the fee
PaymentMethod m = new Upi();
m.pay(80);
m.payWithFee(80);
UPI 80.0 -> PAID
UPI 82.0 -> PAID
Upi never wrote payWithFee. It's the interface's default, which calls pay(82).
Status has exactly three values
package org.studyeasy.s03l18;
public enum Status {
PENDING("waiting"), PAID("done"), DECLINED("refused");
private final String note;
Status(String note) {
this.note = note;
}
public String note() {
return note;
}
}
Status s = Status.valueOf("PAID");
System.out.println(Status.values().length + " " + (s == Status.PAID));
System.out.println(s + ": " + s.note());
String next = switch (s) {
case PENDING -> "wait";
case PAID -> "ship it";
case DECLINED -> "ask for another method";
};
System.out.println(next);
3 true
PAID: done
ship it
The same enum tools as in enums with fields: valueOf, values, a field per constant and a switch with one branch per constant.
A Wallet remembers its balance
package org.studyeasy.s03l18;
public class Wallet extends BasePayment {
private double balance;
public Wallet(double balance) {
super("Wallet");
this.balance = balance;
}
protected boolean approve(double total) {
if (total > balance) {
return false;
}
balance -= total;
return true;
}
public double balance() {
return balance;
}
}
Card and Upi keep nothing between payments; a wallet does. Now two orders use the same wallet:
static class Order {
private final PaymentMethod method;
Order(PaymentMethod method) {
this.method = method;
}
void checkout(double total) {
method.pay(total);
}
}
public static void main(String[] args) {
Wallet wallet = new Wallet(100);
new Order(wallet).checkout(30);
new Order(wallet).checkout(30);
System.out.println("Left: " + wallet.balance());
}
Wallet 30.0 -> PAID
Wallet 30.0 -> PAID
Left: 40.0
Each order keeps a reference to the wallet, not a copy. There's only one Wallet object, so 100 becomes 70, then 40.
The lab also has an inner-class example: a Cart with an inner Summary. The summary is created while the total is 50, the total then changes to 80, and summary.text() prints Total: $80.0. The inner object holds no copy; it reads its outer cart at that moment (see inner classes).
The checkout
package org.studyeasy.s03l18;
public class Lab4_Checkout {
public static void main(String[] args) {
PaymentMethod[] tries = {
new Wallet(50), new Card(), new Upi()
};
Status result = Status.PENDING;
for (PaymentMethod m : tries) {
result = m.pay(80);
if (result == Status.PAID) {
System.out.println("Paid with " + m.name());
break;
}
}
System.out.println("Final: " + result);
}
}
Wallet 80.0 -> DECLINED
Card 80.0 -> PAID
Paid with Card
Final: PAID
The array lists methods in order of preference. The wallet has only 50, so 80 is declined and the loop goes on. The card approves it and break stops the loop, so the Upi is never tried.
Recap
| Piece | Job |
|---|---|
PaymentMethod interface | the contract, plus a default payWithFee |
BasePayment abstract class | shared name and pay; approve left to each child |
| Card, Upi, Wallet | one approve rule each |
Status enum | names the result: PENDING, PAID, DECLINED |
| Shared reference | two orders, one wallet, one balance |
Try it yourself: add a GiftCard that approves only totals under 100, put it first in the checkout array, and predict the output before you run it.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Classes and Objects: One Blueprint, Many Cars
Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.
Java Constructors and the this Keyword
A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.
Java Instance Methods, Getters, Setters and toString
Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.
Java static: Fields, Methods and Blocks Shared by the Whole Class
A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.
