A Comprehensive Guide to Anonymous Class in Kotlin
With code and detailed explanation
A Comprehensive Guide to Anonymous Class in Kotlin
Photo by Thomas Grams on Unsplash
An anonymous class in Kotlin is an instance of an object expression that implements an interface or extends an abstract class without explicitly declaring a named subclass.
It is commonly used for:
- Listeners / Callbacks
- One-time object customization
- Threading / Runnable usage
To fully understand it’s significance and how it helps in separating the business logic and UI-layer code, let’s take an example.
Let us consider an example where we have a navigation feature implemented as a sealed interface.
sealed interface NavigationRoutes {
data object HOME_PAGE: NavigationRoutes
data class DETAIL_PAGE(val detailPageId: String): NavigationRoutes
data class LIST_PAGE(val listPageId: String): NavigationRoutes
}
sealed interface NavigationSubRoutes : NavigationRoutes {
data class HOME_HERO_DASHBOARD_DETAIL(val dashboardDetailPageId: String): NavigationSubRoutes
}
The navigation routes contains routes to HOME PAGE, DETAIL PAGE and LIST PAGE. For HOME PAGE, we have sub-route to HOME DASHBOARD DETAIL PAGE.
For defining the navigation routes, we have an abstract idea, which denotes that we should have an abstract function responsible for navigation. We achieve it by using an abstract class.
abstract class NavigationListener<T : NavigationRoutes>(var navigationRoute: T) {
abstract fun onNavigate(navigationRoutes: T)
fun navigateToPage() {
println("Do some operation before opening page")
onNavigate(navigationRoute)
}
}
The NavigationListener abstract class contains one abstract and a non-abstract method which does some operation before navigating.
Now, we will define our abstract class as an anonymous class to override it’s abstract method and provide a way to navigate based on its NavigationRoutes.
val navigateObj = object : NavigationListener<NavigationRoutes>(NavigationRoutes.HOME_PAGE) {
override fun onNavigate(navigationRoutes: NavigationRoutes) {
when(navigationRoutes) {
is NavigationRoutes.DETAIL_PAGE -> {
println("Opened page with ID: ${navigationRoutes.detailPageId}")
println("On navigated to Detail Page")
navigateToDetailPage()
}
NavigationRoutes.HOME_PAGE -> {
println("On navigated to Home Page")
navigateToHomePage()
}
is NavigationRoutes.LIST_PAGE -> {
println("Opened page with ID: ${navigationRoutes.listPageId}")
println("On navigated to Detail Page")
navigateToListPage()
}
is NavigationSubRoutes.HOME_HERO_DASHBOARD_DETAIL -> {
println("Opened page with ID: ${navigationRoutes.dashboardDetailPageId}")
println("On navigated to Detail Page")
navigateToDashboardDetailPage()
}
}
}
}
private fun navigateToDetailPage() {
println("-----------DetailPage-----------")
}
private fun navigateToListPage() {
println("-----------ListPage-----------")
}
private fun navigateToHomePage() {
println("-----------HomePage-----------")
}
private fun navigateToDashboardDetailPage() {
println("-----------Dashboard Detail Page-----------")
}
On calling navigateToPage() method, defined in the abstract class, it invokes onNavigate(navigationRoute). Since the implementation is in anonymous class, the flow of execution will go to overridden method of navigateObj.
For testing the same, we can write the following snippet in the main() function.
fun main() {
println("Default page is home page")
println("----------------------------------------")
handleNavigation()
println("----------------------------------------")
println("Changing page to detail page")
handleNavigation(NavigationRoutes.DETAIL_PAGE("detail-page-id"))
println("----------------------------------------")
println("----------------------------------------")
println("Changing page to Home dashboard detail page")
handleNavigation(NavigationSubRoutes.HOME_HERO_DASHBOARD_DETAIL("home-dashboard-detail-page-id"))
println("----------------------------------------")
}
fun handleNavigation(navigationRoute: NavigationRoutes = NavigationRoutes.HOME_PAGE) {
navigateObj.apply {
this.navigationRoute = navigationRoute
}.run { navigateToPage() }
}
Following will be its output:
Default page is home page
----------------------------------------
Do some operation before opening page
On navigated to Home Page
-----------HomePage-----------
----------------------------------------
Changing page to detail page
Do some operation before opening page
Opened page with ID: detail-page-id
On navigated to Detail Page
-----------DetailPage-----------
----------------------------------------
----------------------------------------
Changing page to Home dashboard detail page
Do some operation before opening page
Opened page with ID: home-dashboard-detail-page-id
On navigated to Detail Page
-----------Dashboard Detail Page-----------
----------------------------------------
Thus, we delegated the navigation logic to the anonymous class, aggregating all the navigation routes at a single place, making the code robust, cleaner and readable.
Thank you for reading my article, if it gave you a little perspective/clarity, please give a follow / clap as a token of appreciation
메타데이터
- post_id
- 5cb082578eef
- slug
- a-comprehensive-guide-to-anonymous-class-in-kotlin-5cb082578eef
- url
- https://medium.com/@vinayakbhagwani/a-comprehensive-guide-to-anonymous-class-in-kotlin-5cb082578eef
- canonical_url
- https://medium.com/@vinayakbhagwani/a-comprehensive-guide-to-anonymous-class-in-kotlin-5cb082578eef
- author_url
- https://medium.com/@vinayakbhagwani
- status
- ok
- fetched_at
- 2026-07-18 08:34:37