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.
fun reportError(): Nothing { | |
throw RuntimeException() | |
} | |
fun displayHelloMessage(): Unit { | |
println("Hello from Kotlin! :)") | |
} |
📌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.
// primary constructor with fullName property (setter & getter) | |
class Person(val fullName: String) { /*...*/ } | |
// define a secondary constructor | |
class Person(val fullName: String) { | |
val age: Int | |
get() { | |
return 18 | |
} | |
// secondary constructor | |
constructor(fullName: String, age: Int) : this(fullName) {} | |
} | |
// instance of the class | |
val john = Person("John", 24) | |
// init block | |
class Person(val fullName: String) { | |
var isLongName = false | |
init { | |
isLongName = fullName.length > 15 | |
} | |
} |
📌Visibility modifiers
📌Inheritence
- Inheritance: use open keyword for class
- Overriding methods and properties: use the open modifier
open class Person { | |
open val name = "Tom" | |
open fun displaySkills() { } | |
} | |
// inheritance and override | |
class Student : Person() { | |
override val name = "Jerry" | |
override fun displaySkills(){ } | |
} | |
In Kotlin “ object “ keyword can be used to create singleton objects.
object TheObject { | |
fun hello() = "hello" | |
override fun toString() = "Hello, it's me, ${TheObject::class.simpleName}" | |
} | |
fun useSingletonObject() { | |
println(TheObject.hello()) // => hello | |
val someRef: Any = TheObject | |
println(someRef) // => Hello, it's me, TheObject | |
} |
📌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.
abstract class Car { | |
abstract fun run() | |
open fun computeTaxes() {} | |
} | |
abstract class SafeCar: Car() { | |
override fun run() { | |
println("SafeCar is running safely..") | |
} | |
override abstract fun computeTaxes() | |
} |
📌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.
interface Pet { | |
fun eat() | |
fun sleep() | |
} | |
class Cat : Pet { | |
override fun eat() { | |
println("Cat eats fish") | |
} | |
override fun sleep() { | |
println("Cat sleeps a lot") | |
} | |
} |
📌Delegation
- Composition over Inheritance design pattern
- Native support for delegation (implicit delegation)
- Zero boilerplate code
interface PetAction { | |
fun eat() | |
} | |
interface PetColor { | |
val color: String | |
} | |
object YellowColor : PetColor { | |
override val color = "yellow" | |
} | |
class PrintingPetAction(val food: String) : PetAction { | |
override fun eat() { | |
println(food) | |
} | |
} | |
class Cat(petColor: PetColor = YellowColor) : | |
PetAction by PrintingPetAction("eats a lot of fish"), | |
PetColor by petColor | |
fun delegate() { | |
val kittyCat = Cat() | |
println("Pet has color ${kittyCat.color}") | |
kittyCat.eat() | |
} | |
fun main(args: Array<String>) { | |
delegate() | |
} | |
// => Pet has color yellow | |
// => eats a lot of fish |
📌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.
data class Character(val name: String, val age: Int) | |
fun main() { | |
val mickeyMouse = Character("Mickey Mouse", 82) | |
val mickeyMouseToday = mickeyMouse.copy(age = 83) | |
// destructuring declarations | |
val (name, age) = mickeyMouseToday | |
println("$name, $age years of age") | |
mickeyMouseToday.component1() // => name | |
mickeyMouseToday.component2() // => age | |
} |
📌Companion object
- companion object: syntactically it’s similar to the static methods in Java
class Person { | |
companion object { | |
fun callMe() = "Call" | |
} | |
} | |
// Person.callMe() |
📌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.
enum class Color(val value: Int) { | |
GOLD(0xffd323), | |
SILVER(0xeaeaea), | |
WHITE(0xffffff), | |
BLACK(0x000000), | |
RED(0xFF0000) | |
} | |
fun printProperty() = println(Color.GOLD.value) // => 16765731 | |
fun printName() = println(Color.GOLD.name) // => GOLD | |
fun printPosition() = println(Color.GOLD.ordinal) // => 0 |
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)