How to write Classes in Kotlin? Kotlin Class and Inheritance Tutorial

Опубликовано: 28 Сентябрь 2024
на канале: Ludwig Dickmanns
2,552
75

Classes and inheritance in Kotlin!

In this video, you're going to learn about the basics of classes in Kotlin. Firstly, we're going to create a simple class. Secondly, we're going to add some variables and a function to it. Finally, we're going to extend the class with a subclass and discuss inheritance. Classes are created with the class keyword, followed by the class name and the (optional) primary constructor. Then follows the (optional) class body. You can add variables to a class in the primary constructor or in the class body, and, in Kotlin, they're called properties. Further, classes can have functions. You can also extend classes with subclasses. This is called inheritance, and, thereby, you get access to the properties and functions of the superclass. However, Kotlin classes are final by default. Therefore, you have to declare your class explicitly as extensible with the 'open' keyword. The same goes for functions. If you want to override a function of a class, this function must be declared as open. Of course, functions you define in a subclass cannot be accessed by the superclass.

00:00 Intro
00:23 Creating a Class
00:47 Variables/Properties
02:52 Functions
04:20 Inheritance
06:30 Overriding

Documentation for the Function Basics in Kotlin:
https://kotlinlang.org/docs/basic-syn...

IntelliJ IDEA Community Version:
https://www.jetbrains.com/idea/

@kotlin.learnings on Instagram:   / kotlin.learnings  
@java.learnings on Instagram:   / java.learnings  

Thank you for watching!