Browsing tag:

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 Function Overriding Rules

Overridden functions of an open class is open by default and we can stop that by using the final Keyword Meaning we can override and inherit overridden method by default. Let’s say we built a learning app. Course is our abstract class, KotlinCourse inherits or extends Course, SpecialKotlinCourse also inherits or extend KotlinCourse. As you can see from the code […]

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: 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, […]

OOP: What is an Interface?

An interface is a collection of abstract methods. It is a reference type in Java/Kotlin. It is similar to class. A class implements an interface, thereby inheriting the abstract methods of the interface. [tabs type=”horizontal”] [tabs_head] [tab_title] Java Code [/tab_title] [tab_title] Kotlin Code [/tab_title] [/tabs_head] [tab] public class HelloWorld { class Example implements InterfaceExample{ @Override public […]