Protocols and Multimethods for Polymorphism
Clojure offers two flexible polymorphism tools that complement macros and modular design. This lesson teaches protocols and multimethods for extensible, decoupled code.
Protocols and Multimethods for Polymorphism is a free Clojure Functional Programming & JVM Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Clojure Functional Programming & JVM Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Polymorphism in Clojure
Clojure separates data from behavior. Protocols and multimethods let you dispatch behavior without classes or inheritance.
Defining a Protocol
A defprotocol names a set of method signatures, like an interface.
(defprotocol Shape
(area [s])
(perimeter [s]))Implementing With a Record
Use defrecord to create a type that implements the protocol.
(defrecord Circle [r]
Shape
(area [_] (* Math/PI r r))
(perimeter [_] (* 2 Math/PI r)))
(println (area (->Circle 2)))Dispatch on Type
Protocols dispatch on the type of the first argument. This is fast and the most common polymorphism in Clojure.
Extending Existing Types
Use extend-protocol to add behavior to types you do not own, even built-in ones.
(extend-protocol Shape
java.lang.Long
(area [n] (* n n)))
(println (area 5))When You Need More
Protocols only dispatch on type. When you must dispatch on a value, multiple arguments, or a computed key, reach for multimethods.
Defining a Multimethod
defmulti takes a dispatch function; defmethod supplies an implementation per dispatch value.
(defmulti describe :kind)
(defmethod describe :dog [_] "woof")
(defmethod describe :cat [_] "meow")
(println (describe {:kind :dog}))The Default Method
Provide a :default method to handle unmatched dispatch values gracefully.
(defmethod describe :default [_] "unknown")
(println (describe {:kind :fish}))Dispatch on Anything
The dispatch function can compute any value, even from multiple arguments, giving multimethods far more flexibility than protocols.
(defmulti combine (fn [a b] [(:type a) (:type b)]))
; dispatch on a pair of typesChoosing Between Them
Prefer protocols for type-based, performance-sensitive dispatch. Use multimethods when dispatch logic is richer than a single type.
Open for Extension
Both tools are open: new types or methods can be added later in separate namespaces without editing the original code, solving the expression problem cleanly.
Quick Check
Test your polymorphism knowledge.
Recap
You learned protocols with defrecord and extend-protocol for type dispatch, multimethods with defmulti/defmethod for value dispatch, default methods, and how both keep code open for extension.
Frequently asked questions
Is the “Protocols and Multimethods for Polymorphism” lesson free?
Yes — the full text of “Protocols and Multimethods for Polymorphism” is free to read here on the web, and the Clojure Functional Programming & JVM Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Clojure Functional Programming & JVM Backend Development course, upgrade to CoddyKit PRO.
What will I learn in “Protocols and Multimethods for Polymorphism”?
Clojure offers two flexible polymorphism tools that complement macros and modular design. This lesson teaches protocols and multimethods for extensible, decoupled code. You practise Clojure Functional Programming & JVM Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Clojure Functional Programming & JVM Backend Development?
No prior experience is required. Clojure Functional Programming & JVM Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Protocols and Multimethods for Polymorphism” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Clojure Functional Programming & JVM Backend Development lesson?
Yes. Every Clojure Functional Programming & JVM Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding Clojure Macros
- Defining & Using Namespaces
- Organizing Code with Modules
- Protocols and Multimethods for Polymorphism