
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin String - toString() Function
The Kotlin string toString() function is used in kotlin when we override the "toString()" method in a class. It helps us to provide the custom string representation of object of that class.
When to use open override fun toString()
There are the following use cases of this function −
- Customize Object Description: Overriding toString() is useful when we want object of a class to return a meaningful representation.
- Inheritance Support: We need to mark the toString() class as open. If you want the subclass to inherit the string representation of the parent class.
Syntax
Following is the syntax of the Kotlin string toString() function −
open override fun toString(): String
Syntax and Usage
- open: This keyword helps the subclass to override the toString() function. A function cannot be overridden without the "open" keyword.
- override: This keyword helps to specify that the function is overriding a method from the parent class.
Parameters
This function does not accepts any parameters.
Return value
This function returns a string.
Example 1: Default Behaviour vs. Overridden Behaviour
Let us use the default toString() function in Kotlin −
class Example fun main() { val example = Example() println(example.toString()) }
Output
If you do not override the toString() function, it will return the class name and the hash code of the object −
Example@3941a79c
Here, let's use the overridden toString() function −
class Example(val name: String, val age: Int, val city: String) { // Overriding the toString() method override fun toString(): String { return "Person(name='$name', age=$age, city='$city')" } } fun main() { val person = Example("Aman", 25, "Hyderabad") println(person.toString()) // `toString()` is called implicitly when printing objects println(person) }
Output
If we use overridden toString() Provides meaningful details about the object, making the output human-readable.
Person(name='Aman', age=25, city='Hyderabad') Person(name='Aman', age=25, city='Hyderabad')
Example 2: Custom implementation of toString()
In this example, we create two classes overriding the class using the toString() function, displaying the details of both classes.
open class Animal(val name: String, val species: String) { open override fun toString(): String { return "Animal(name='$name', species='$species')" } } class Dog(name: String, species: String, val breed: String) : Animal(name, species) { override fun toString(): String { return "Dog(name='$name', species='$species', breed='$breed')" } } fun main() { val animal = Animal("Lion", "Mammal") println(animal.toString()) val dog = Dog("Buddy", "Canine", "Golden Retriever") println(dog.toString()) }
Output
Following is the output −
Animal(name='Lion', species='Mammal') Dog(name='Buddy', species='Canine', breed='Golden Retriever')