Skip to main content
ubuntuask.com

Back to all posts

How to Pass A Class As A Function Parameter In Kotlin?

Published on
4 min read
How to Pass A Class As A Function Parameter In Kotlin? image

Best Kotlin Programming Books to Buy in October 2025

1 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
2 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
7 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.62
Kotlin: An Illustrated Guide
9 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
+
ONE MORE?

In Kotlin, you can pass a class as a function parameter by using the KClass type. The KClass type is a Kotlin built-in representation of a class or a type. To pass a class as a function parameter, you can define the function parameter with the KClass type followed by the name of the class you want to pass.

For example, if you want to pass the Person class as a function parameter, you can define the function like this:

fun printClassName(clazz: KClass<*>){ println(clazz.simpleName) }

// Calling the function with the Person class printClassName(Person::class)

In the above example, the printClassName function takes a parameter of type KClass and prints the name of the class passed to it. When calling the function, you can pass the Person class using the ::class syntax.

How to implement inheritance in Kotlin classes?

In Kotlin, inheritance is implemented using the : symbol to denote that a subclass is inheriting from a superclass. Here's an example of how to create a simple class hierarchy with inheritance in Kotlin:

// superclass open class Vehicle(val brand: String, val model: String) { fun drive() { println("The $brand $model is driving") } }

// subclass inheriting from Vehicle class Car(brand: String, model: String, val color: String) : Vehicle(brand, model) { fun honk() { println("The $brand $model is honking") } }

fun main() { val myCar = Car("Toyota", "Camry", "red") myCar.drive() myCar.honk() }

In this example, Vehicle is a superclass with a drive method, and Car is a subclass that inherits from Vehicle and adds a honk method. When creating an instance of Car, you still need to pass the required parameters to the superclass constructor (Vehicle(brand, model)), as shown in the Car class definition.

When you run the main function, you should see the following output:

The Toyota Camry is driving The Toyota Camry is honking

This demonstrates how inheritance works in Kotlin classes. You can create more complex class hierarchies with multiple levels of inheritance by chaining superclass constructors in the same way.

What is the primary constructor in Kotlin classes?

The primary constructor in Kotlin classes is defined within the class header itself, directly after the class name. It allows you to specify the properties and initial values of the class. It is called the primary constructor because it is the main constructor used to create instances of the class.

How to define abstract classes in Kotlin?

In Kotlin, we can define an abstract class using the abstract keyword. Abstract classes are classes that cannot be instantiated and can contain abstract functions that must be implemented by their subclasses.

Here's an example of how to define an abstract class in Kotlin:

abstract class Shape { abstract fun calculateArea(): Double abstract fun calculatePerimeter(): Double

// Non-abstract functions can also be included in abstract classes
fun printShapeDetails() {
    println("Area: ${calculateArea()}")
    println("Perimeter: ${calculatePerimeter()}")
}

}

class Circle(val radius: Double) : Shape() { override fun calculateArea(): Double { return 3.14 * radius * radius }

override fun calculatePerimeter(): Double {
    return 2 \* 3.14 \* radius
}

}

fun main() { val circle = Circle(5.0) circle.printShapeDetails() }

In this example, Shape is an abstract class with two abstract functions calculateArea() and calculatePerimeter(). The Circle class is a subclass of Shape that provides implementations for these abstract functions. The main function creates an instance of Circle and calls the printShapeDetails() function to print the area and perimeter of the circle.

How to implement delegation in Kotlin classes?

In Kotlin, delegation can be implemented using the by keyword. Here is a step-by-step guide on how to implement delegation in Kotlin classes:

  1. Create an interface that contains the method(s) you want to delegate to another class. For example:

interface Printer { fun printMessage(message: String) }

  1. Create a class that implements the interface and provides the behavior for the method(s). For example:

class ConsolePrinter : Printer { override fun printMessage(message: String) { println(message) } }

  1. Create another class that you want to delegate the behavior to. This class will have a property of type Printer using the by keyword. For example:

class DelegatedPrinter(private val printer: Printer) : Printer by printer

  1. Now you can use the DelegatedPrinter class to delegate the behavior of the printMessage method to an instance of Printer class. For example:

fun main() { val consolePrinter = ConsolePrinter() val delegatedPrinter = DelegatedPrinter(consolePrinter)

delegatedPrinter.printMessage("Hello, world!")

}

When you run the above code, it will delegate the printMessage method call to the ConsolePrinter class, which will then print the message to the console.

This is a simple example of how delegation can be implemented in Kotlin classes. Delegation allows you to compose classes together to reuse behavior and keep your code clean and modular.