expect/actual Classes & Properties
Beyond functions: declarations you can expect/actual.
expect/actual Classes & Properties is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 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 Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Functions
expect/actual is not limited to functions. You can also declare whole classes, properties, and even objects that differ per platform.
An expect Property
Need a value that each platform defines? Declare an expect val in commonMain and let every target supply the actual value.
expect val platformName: StringEach Target Sets It
Android and iOS each provide an actual val. The property reads like a constant in shared code but resolves per platform.
actual val platformName: String = "iOS"An expect Class
For richer behavior, declare an expect class. It lists the members shared code can use, while each platform fills in the bodies.
expect class Platform() {
val name: String
}The Android actual Class
The androidMain actual class implements every declared member. Here name returns an Android-specific string.
actual class Platform actual constructor() {
actual val name = "Android"
}Match Members Exactly
Each actual class must declare every member the expect promised, with the same names and types. Missing one is a compile error.
Constructors Need actual Too
If the expect class has a constructor, mark the platform one as an actual constructor. The compiler checks that they line up.
Use It Like Any Class
In shared code you just create and use it. The right actual backs the object, so common logic stays unaware of the platform.
val name = Platform().nameAdd Platform-Only Extras
An actual class may add extra private members beyond the expect. Only what the expect declares is visible to shared code.
Prefer the Smallest Tool
Use an expect property for a value, an expect function for an action, and an expect class only when you need state plus behavior. ✨
A Familiar Modern Note
Recent Kotlin nudges you toward interfaces plus expect/actual functions over expect classes, but classes still work well today.
Quick Check
Consider what an actual class owes its expect.
Recap
You saw that expect/actual covers properties and classes too. Declare the shape in commonMain, then fulfill every member in each platform's actual. 🎉
Frequently asked questions
Is the “expect/actual Classes & Properties” lesson free?
Yes — the full text of “expect/actual Classes & Properties” is free to read here on the web, and the Kotlin Multiplatform Academy 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 Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “expect/actual Classes & Properties”?
Beyond functions: declarations you can expect/actual. You practise Kotlin Multiplatform Academy 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 Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “expect/actual Classes & Properties” 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 Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy 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
- Why expect/actual Exists
- Get the Platform Name per Target
- expect/actual Classes & Properties
- Common Mistakes & Compiler Errors