← Back to Week 5

Class Communication Walkthrough

Step through main as it orchestrates a card game. Notice main never does any card work itself — it just connects the right objects.

Step 0 of 6
Click Start to walk through how Main, Deck, Hand, and Card all collaborate.
// MainApp.java
public class MainApp {
public static void main(String[] args) {
Deck deck = new Deck();
Hand hand1 = new Hand();
// deal 5 cards
for(int i = 0; i < 5; i++) {
Card card = deck.deal();
hand1.deal(card);
}
int handValue = hand1.getValue();
System.out.println("Hand value: " + handValue);
}
}
Heap — live objects
Console
 
The big idea: main doesn't shuffle anything. It doesn't add up card values. It doesn't track what's left in the deck. Each class does its own job. main is just the conductor — it tells Deck to deal, hands the result to Hand, asks Hand for the total. This is what people mean by "separation of concerns."
← Has-A Relationship Back to Week 5 →