Classes in Kotlin
Tags: gde, android, learnkotlin, learnkotlinfromgde
Published: 2020-02-02 16:32:13

---
title: Classes in Kotlin
published: true
date: 2020-02-02 15:31:56 UTC
tags: gde,android,learnkotlin,learnkotlinfromgde
canonical_url: https://medium.com/@magdamiu/classes-in-kotlin-9cf584556431
---

The journey in Kotlin Wonderland continues with an article about classes and objects. Until now we discovered details about Kotlin philosophy, basic types, control flow expressions, null safety and functions.

![](https://cdn-images-1.medium.com/max/960/0*-L65lAeNu3N0zYcX)

### 📌Class Hierarchy

![](https://cdn-images-1.medium.com/max/960/0*lMESr4bOqnINsW41)

**Unit** :

Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.

**Nothing:**

Nothing is a type in Kotlin that represents “a value that never exists”, which means “no value at all”.

Nothing can be used as the return type of a function that never returns the code execution — like, looped forever or always throws _Exception_. Don’t get confused because Java does not have anything similar to the _Nothing_ type.

{% gist https://gist.github.com/magdamiu/7e4ba9f4ad5594a1fd48740931a6bb9f %}

### 📌Classes and Objects in Kotlin

- Classes and objects in Kotlin work the same way as in most object-oriented languages: a _class_ is a blueprint, and an _object_ is an instance of a class.
- There are primary and secondary constructors. For secondary we should add the keyword **constructor**
- The primary constructor cannot contain any code.
- Initialization code can be placed in initialization blocks, which are prefixed with the **init** keyword. The initialization blocks are executed in the same order as they appear in the class body.

{% gist https://gist.github.com/magdamiu/31c4df7e3bfb5c18c6741a6a3af85a51 %}

### 📌Visibility modifiers

![](https://cdn-images-1.medium.com/max/847/0*mFQ0NeEm1QRAIOXG)

### 📌Inheritence

- Inheritance: use **open** keyword for class
- Overriding methods and properties: use the **open**  modifier

{% gist https://gist.github.com/magdamiu/7e7823ea53c61b9b8c1dc5c4e1aef579 %}

In Kotlin “ **object** “ keyword can be used to create singleton objects.

{% gist https://gist.github.com/magdamiu/2579841e55fe0f7e7ea81d19b7baef7e %}

### 📌Abstract class

- An abstract class cannot be instantiated.
- We can override a non-abstract open member with an abstract one
- Abstract class or abstract function does not need to annotate with open keyword as they are _open by default_.

{% gist https://gist.github.com/magdamiu/01a0fddd3bdcd33fa585c8cca98d0afc %}

### 📌Interface

- An interface can have both abstract and non-abstract functions.
- An interface can only have abstract properties (data members)
- A class can implement more than one interface.
- All abstract properties and abstract functions of an interface must be overridden in the classes that implement it.

{% gist https://gist.github.com/magdamiu/0c418d700466f4336e2626d7a013396b %}

### 📌Delegation

- Composition over Inheritance design pattern
- Native support for delegation (implicit delegation)
- Zero boilerplate code

{% gist https://gist.github.com/magdamiu/7f4790df22482b08359a5fb6f420e859 %}

### 📌Data classes

- Data classes are a concise way to create classes that just hold data.
- Kotlin makes working with immutable data objects easier by automatically generating a copy() function for all the data classes.
- Explicit implementations for componentN() and copy() functions are not allowed.
- The data keyword provides functions that allow _destructuring declarations _. In short, it creates a function for every property.
- A data class cannot extend from another data class but it may extend other classes.
- A data class can’t be abstract, open, sealed or inner.

![](https://cdn-images-1.medium.com/max/361/0*LaAunrpjPCZa0fa8)

{% gist https://gist.github.com/magdamiu/8154994950a816cd9e3bd6a55189036f %}

### 📌Companion object

- **companion** object: syntactically it’s similar to the _static_ methods in Java

{% gist https://gist.github.com/magdamiu/9637eca09562274cdbf58cfcd1937153 %}

### 📌Enum classes

- Enum constants aren’t just mere collections of constants — these have properties, methods etc
- Each of the enum constants acts as separate instances of the class and separated by commas.
- Enums increases readability of your code by assigning predefined names to constants.
- An instance of enum class cannot be created using constructors.

{% gist https://gist.github.com/magdamiu/1f5e6beb5e6547d2fa438407c983db2b %}

Check here my previous articles about Kotlin:

- [Hello Kotlin](https://dev.to/magdamiu/hello-kotlin-3kof-temp-slug-9275067)
- [Kotlin Basics](https://dev.to/magdamiu/kotlin-basics-35pe-temp-slug-3968702)
- [Kotlin functions are fun](https://dev.to/magdamiu/kotlin-functions-are-fun-20em-temp-slug-6448630)

_This post was originally published at_ [_http://magdamiu.com_](https://magdamiu.com/2020/02/02/classes-in-kotlin/) _on February 2, 2020._

Enjoy and feel free to leave a comment if something is not clear or if you have questions. And if you like it please 👏 and share !

Thank you for reading! 🙌🙏😍✌

Follow me on Twitter [Magda Miu](https://twitter.com/magdamiu)