Soumak De has Published 84 Articles

Equivalent of getClass() for KClass in Kotlin

Soumak De

Soumak De

Updated on 01-Mar-2022 10:56:29

196 Views

In this article, we will take an example and demonstrate how we can obtain the class reference in Kotlin. Kotlin does not support fetching the class reference directly, but you can obtain the same reference via extension itself. In the following example, we will see how we can do it ... Read More

How to extend and implement at the same time in Kotlin?

Soumak De

Soumak De

Updated on 01-Mar-2022 10:51:36

530 Views

In this article, we will take an example to demonstrate how we can extend and implement in the same class. In this example, We will be creating an interface and a dummy Parent class.From the Child class, we will extend the Parent class and implement the interface.Example – Extending and ... Read More

Why can't 'kotlin.Result' be used as a return type?

Soumak De

Soumak De

Updated on 01-Mar-2022 10:44:58

955 Views

Result is a Serializable class in Kotlin. The function definition looks like this −class Result : SerializableThis class has two properties − "isFailure" and "isSuccess".As per the documentation, Result cannot be used as a direct return type of Kotlin function. However, in this article, we will see how we can ... Read More

What's the difference between "!!" and "?" in Kotlin?

Soumak De

Soumak De

Updated on 01-Mar-2022 10:38:44

748 Views

In this article, we will take an example and demonstrate the difference between (!!) and (?) in Kotlin.Example – "!!" and "?" operator in KotlinKotlin provides a wonderful operator to check NULL pointer exceptions. It throws a NULL pointer exception instead of breaking the programming logic whenever the variable is ... Read More

How to throw custom exception in Kotlin?

Soumak De

Soumak De

Updated on 01-Mar-2022 10:33:05

264 Views

Exception is an important aspect of any programming language. It prevents our code from generating incorrect output at runtime. There are two types of exceptions −Checked exceptionsUnchecked exceptionsChecked ExceptionsChecked exceptions are those which are checked at the compile time. As per example, FileNotFoundException() or IOException. In the following example, we ... Read More

Override getter for Kotlin data class

Soumak De

Soumak De

Updated on 01-Mar-2022 09:59:38

2K+ Views

Data class is a collection in Kotlin to store data. Logically this is same as creating a Java POJO class. In Java, we do create extra member functions to set and get the data in the member variables. In Kotlin, we don’t need to create separate methods to access the ... Read More

What does 'by' keyword do in Kotlin?

Soumak De

Soumak De

Updated on 23-Nov-2021 07:25:10

3K+ Views

Kotlin supports delegation of design pattern by introducing a new keyword "by". Using this keyword or delegation methodology, Kotlin allows the derived class to access all the implemented public methods of an interface through a specific object.ExampleIn this example, we will implement an abstract method of a Base class from ... Read More

What is "out" keyword in Kotlin?

Soumak De

Soumak De

Updated on 23-Nov-2021 07:23:31

861 Views

"Out" keyword is extensively used in Kotlin generics. Its signature looks like this −ListWhen a type parameter T of a class C is declared out, then C can safely be a super type of C. That means, a Number type List can contain double, integer type list.ExampleThe following example demonstrates how ... Read More

How to read a text file from resources in Kotlin?

Soumak De

Soumak De

Updated on 23-Nov-2021 07:18:26

2K+ Views

In this article, we will see how we can read a text file using Kotlin library functions. Kotlin is based on Java, hence we can use different Java library functions in Kotlin too.Example - BufferedReaderGo ahead and create a Kotlin file in your workspace and name it " ReadFile.kt". Keep ... Read More

What does "?:" do in Kotlin? (Elvis Operator)

Soumak De

Soumak De

Updated on 23-Nov-2021 07:16:42

511 Views

Elvis operator is very common in many programming languages. This is a binary expression that returns the first operand when the expression value is True and it returns the second operand when the expression value is False. Generally, the Elvis operator is denoted using "?:", the syntax looks like −First ... Read More

Previous 1 ... 3 4 5 6 7 ... 9 Next
Advertisements