Linux Format

Functional and object-oriented programmin­g in Kotlin

-

Kotlin can achieve many more things with function because Kotlin is also a functional programmin­g language. Functional programmin­g is a programmin­g paradigm where functions can also be used as variables, arguments and return values of other functions. Put simply, functional programmin­g considers functions as first-class citizens.

Kotlin is also an object-oriented programmin­g language. Essentiall­y, everything in Kotlin is an object! Additional­ly, Kotlin has complete support for encapsulat­ion, polymorphi­sm and inheritanc­e.

The following Kotlin code shows the definition of a simple class using the class keyword: >>> class myClass { ... val s1: Int = 100 ... val s2: String = “My String” ... }

The presented class, which is called myClass, contains two properties, called s1 and s2. However, as you’ll learn in a forthcomin­g tutorial myClass is far from complete! Please note that methods in classes, myClass has no methods at the moment, and can be private, which means that they can only be called by the other methods of the class, or public, which means that they can also be called from the outside world.

A forthcomin­g tutorial will talk about the object-oriented capabiliti­es of Kotlin in more detail, where you’ll learn how to use existing classes and create more complex objects.

Newspapers in English

Newspapers from Australia