Kotlin Program to Add Two Integers In this program, you’ll learn to store and add two integer numbers in Kotlin. After addition, the final sum is displayed on the screen. Example: Kotlin Program to Add Two Integers fun main(args: Array<String>) { val first: Int = 10 val second: Int = 20 val sum = first …
Kotlin Program to Print an Integer (Entered by the User) In this program, you’ll learn to print an integer entered by the user. The integer is stored in a variable and printed to the screen using nextInt() and println() functions respectively. Example 1: How to Print an Integer entered by an user in Kotlin using …
Kotlin Extension Function In this article, you will learn to extend a class with new functionality using extension functions. Suppose, you need to extend a class with new functionality. In most programming languages, you either derive a new class or use some kind of design pattern to do this. However, in Koltin, you can also use extension …
Kotlin Companion Objects In this article, you will learn to create and use companion objects in your Kotlin program with the help of examples. Before taking about companion objects, let’s take an example to access members of a class. class Person{ fun callMe() = println(“I’m called.”) } fun main(args: Array<String>) { val p1 = Person() …
Kotlin Object Declarations and Expressions In this article, you will learn about object declarations (singletons) and object expressions with the help of examples. Object Declarations Singleton is an object-oriented pattern where a class can have only one instance (object). For example, you are working an application having SQL database backend. You want to create a …
Kotlin Sealed Classes In this article, you will learn about Sealed class, how they are created, and when to use them with the help of examples. Sealed classes are used when a value can have only one of the types from a limited set (restricted hierarchies). Before going into details about sealed classes, let’s explore …
Kotlin Data Class In this article, you will learn to create data classes in Kotlin. You will also learn about requirements that data class must fulfill, and their standard functionalities. There may arise a situation where you need to create a class solely to hold data. In such cases, you can mark the class as …
Kotlin Nested and Inner Class In this article, you will learn to work with nested and inner classes with the help of examples. Kotlin Nested Class Similar like Java, Kotlin allows you to define a class within another class known as nested class. class Outer { … .. … class Nested { … .. … …
Kotlin Interfaces In this article, you will learn about interfaces and how to implement it in Kotlin with the help of examples. Kotlin interfaces are similar to interfaces in Java 8. They can contain definitions of abstract methods as well as implementations of non-abstract methods. However, they cannot contain any state. Meaning, interface may have …
Kotlin Abstract Class In this article, you will learn about abstract class and how to implement it in Kotlin (with the help of examples). Like Java, abstract keyword is used to declare abstract classes in Kotlin. An abstract class cannot be instantiated (you cannot create objects of an abstract class). However, you can inherit subclasses from …