DEV Community

Magda Miu
Magda Miu

Posted on • Originally published at Medium on

Classes in Kotlin

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.

📌Class Hierarchy

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.

📌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.

📌Visibility modifiers

📌Inheritence

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

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

📌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.

📌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.

📌Delegation

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

📌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.

📌Companion object

  • companion object: syntactically it’s similar to the static methods in Java

📌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.

Check here my previous articles about Kotlin:

This post was originally published at http://magdamiu.com 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

Top comments (0)