Browsing category:

Kotlin

Kotlin Data Classes

In this tutorial we are going to discuss kotlin data classes, and show some examples on how you can use them in your app. We frequently create classes whose main purpose is to hold data. In such a case some standard functionality and utility functions are often mechanically derivable from the data.

Kotlin Interface Examples

Interface define contract for classes. An interface is similar to a class just that, it’s a collection of abstract methods and variables. Essentially Interfaces are one level more abstract than abstract classes. Let’s say we have an interface called Driveable. interface Driveable { fun drive() // declaring ‘abstract’ is implicit and unnecessary } we can then have […]

Kotlin: Open & Abstract Classes and Inheritance

In object-oriented programming, inheritance enables new objects to take on the properties of existing objects. We can have class that can inherit properties and methods from the parent class, when we inherit classes, we do not need to specify the method and properties again, in the new class (child class or sub class). In essence it allow us to […]

Kotlin: Class Examples

Class without a Constructor but with default variable (or properties or data) [box type=”info” align=”” class=”” width=””]In class-based object-oriented programming, a constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.[/box] class Person { […]

Kotlin: Function or Methods Example

Simple Function. neither accepts nor return any variable or value fun helloWorld(){ println(“Hello World”) }  Function that takes a parameter but returns no value fun stringFunction (text : String){ for (c in text){ println(“$c “) //KOTLIN -> K O T L I N } } fun intFunction(int : Integer){ println(int) } Functions that returns a […]

Kotlin: Break and Continue Statements

Break and Continue Statements allow us to define jumps in our code and they are used inside Loops breakstatement allow us to jump to the end of the loop and halt any further iterations. This is useful whenever we  are interested in the first occurrence of something. val string = “Kotlin” for (char in string){ […]

Kotlin ArrayList and Loops Example

Kotlin ArrayList Kotlin ArrayList is also very similar to Java Arraylist. ArrayList<T> is the resizable-array implementation of the List interface. The ArrayList class has only a few methods in addition to the methods available in the List interface. val myArrayList = arrayListOf(“John”, “Fred”, “McKinsey”, “Morgan”, “Sarah”) //an arraylist of my friends val newFriendList = arrayListOf(“Musa”, “Victor”, “Timothy”) myArrayList.add(0, […]

Kotlin: Array vs List – Similarities and Differences

// Initializing array and list val array = arrayOf(1, 2, 3) val list = listOf(“apple”, “ball”, “cow”) val mixedArray = arrayOf(true, 2.5, 1, 1.3f, 12000L, ‘a’) // mixed Array val mixedList = listOf(false, 3.5, 2, 1.4f, 13000L, ‘b’) // mixed List Arrays An array is a container object that holds a fixed number of values […]

  • 1
  • 2