Check out a free preview of the full Introduction to Kotlin and Android Development course:
The "Classes" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to declare a class with the class name and the optional header and body and discusses constructors. A class in Kotlin can have a primary constructor and one or more secondary constructors.

Get Unlimited Access Now

Transcript from the "Classes" Lesson

[00:00:00]
>> OOP, because we are working with classes, we're working with OOP. Classes, we have full support for OOP because remember, this compiles to Java as I was mentioning before. So because we are actually compiling to some kind of a Java VM, we are using OOP. So unfortunate, but on the same, I don't wanna say unfortunate, we're are not going to use a lot of OOP in JetPack Composable.

[00:00:25] So you will see that it's not based on options, okay? So first, how to grade the class? It's not as straightforward when you look at this for the first time, okay? I know what you're looking at this and you're saying, hey, Max, you're lying to me, you're lying to us.

[00:00:47] Because it looks like I understand what's going on there, I have a class with a name person and then they have a property. It says public id integer with a default value, and then they have a function or a method, and we have this, okay? So that part is a straightforward, I think you can get it, but it gets weird when we move one step.

[00:01:09] First, there is no new keywords, so if you wanna create a new person, it's not new person, it's just person. The capital P let me know as a reader of this code that this is not a function, okay? It's a constructor of something, because it's title case. It starts with uppercase, with the capital P, okay?

[00:01:35] So far so good, okay, but what if I create a new class user extending from person? By the way, how do we say extends from? Colon, so no extends keyword, just colon. It doesn't work because by default, classes are closed. So you cannot inherit from other class, which is kind of weird, right?

[00:01:57] So when they created Kotlin, they did a research on open source repositories with Java code. And they saw what's the normal situation, what's the most common situation. The most common situation with classes, we don't inherit classes. So they say, okay, let's do that by default. So by default, you cannot inherit from person, unless you open it.

[00:02:24] So you need to explicitly open a class if you want inheriting from that class, okay? Also, you can open methods, so you can override those methods. Again, I'm giving you the first site, but we are not going to use this a lot because JetPack Compose, the UI frame that we will use is not based on OOP.

[00:02:51] But we may use some of these techniques anyway at some point. So we can open a class and then we can extend from that class, for example, here I have DBRecord that is extending from this one. And if you pay attention here, when we extend, we don't just use the name of the superclass.

[00:03:15] We are calling the constructor. So those parentheses there are actually calling the constructor. We will see some examples of this later anyway, okay? So constructors, let's talk about constructors. This is interesting. So I'm creating a new class here. It's open, so I can subclass it, call request. And look at this, first, where is the code look for the class?

[00:03:44] Where is the curly braces, where are the curly braces? I don't have any, I'm creating an immutable variable here directly in the name of the class. This is called the primary constructor, so we don't just have constructors. We have primary constructors, we have secondary constructors, and we have initializer blocks.

[00:04:11] So a primary constructor is the one that you set directly in the name of the class, and you can receive one or more variables. Like Int, if you add the prefix val or var, it also creates a property, and auto sets the property from its value. Does it make sense?

[00:04:34] So when I'm here, I'm setting an id and then a url. Look at these hints, remember those hints are just hints, they're not there. Can I call these but with passing the name? Yeah, even in the constructor, I can also name the arguments, okay? So in this case id, it's being received but I'm not doing anything with that.

[00:05:03] It's not creating a property, okay, because it doesn't contain var or val. If I add a val var, val mutable, val immutable, now R will have an id. Okay, so I can print it's id and it's url. So it's creating a property, receiving that property in the constructor, in the primary constructor and saving it in a value for later usage.

[00:05:29] And if you don't have any more properties or functions, that's all, you don't need to open curly braces, okay? Maybe you're thinking, that doesn't seem so useful. But there are many cases where you just need to create a little structure for some data and that's all, well, simple, okay?

[00:05:50] And we have initializer blocks. An initializer block is just an init block, there are no parentheses in the init block. And it's just a code that is going to be executed after the object is implemented. After you create an object, an instance of this class, it will just call init.

[00:06:11] It doesn't matter if it's coming from here or not. And by the way, look at constructors here in the signature, this takes time, okay? When you're signing, you get used to this. So the superclass receives a URL in the argument. Well, actually, I can just change the primary constructor.

[00:06:31] So now we receive an id also. So here I'm extending from this constructor. So my primary constructor is extending from my superclass primary contractor, I'm executing its constructor. And right now, I'm passing a 0 as a first argument on the URL that I have here, I'm passing that URL as a second argument there, Okay?

[00:06:59] It feels weird, but you get used to that. And what about other constructors? Well, you can create secondary constructor with the constructor key. So you have the constructor keyword and different argument. So for example, here you can construct this user class in three ways. The primary constructor, that is actually passing an id, the name, or id and name.

[00:07:28] Three ways to construct this. Does it make sense? Question for you, how many properties do I have in user?
>> Three.
>> Three.
>> One.
>> One, we have two. I have name, that is actually clearly defined here as a property, and id because it has a bow in the primary constructor.

[00:07:57] So it's also property. Sometimes we write this like this, Or you can actually do this if you want. So typically, we put it like that. So if you receive properties here, so you can see all the properties, the ones that in the primary constructor and the other properties align.

[00:08:22] Let's remove the private, so maybe we'll become clearer, like that. So how many properties? Well, I have two. One is coming from the constructor and the other one is clear outside, okay? And that's all for now in terms of classes, okay? It's not a big deal. And before jumping into JetPack Compose and start creating our app, the final part I wanna show you quickly, because we're not going to use this a lot.

[00:08:52] Is some other type that you can use in copying that might not be available on every other language. Interfaces, that's actually straightforward, if you know any other OOP language such as Java, C#. An interface is just a list of functions or properties that then you can implement and then you're forced to implement.

[00:09:14] For example, here I have an interface called Listener that has one function, no code in the interface. And then I can have a class implementing the interface. So it's actually forced to override that function and put some code on it. So this is similar to Java, nothing really fancy.

[00:09:34] Objects, so it's common in Java that sometimes you need to create an object. But you don't wanna create more instances of that class, but you need to create the class anyway. Or you create an anonymous class, there's several ways to do that. In JavaScript, you create objects, typically with JSON.

[00:09:55] So in JavaScript, you create an object, okay, and you use JSON. My name, you don't need to create a class and then construct an object of that class, you just create an object of type object, okay? Well, in Kotlin, we can also do that, with the keyword object.

[00:10:13] So objects are not classes. So you cannot construct more objects of that class. It's just a real object without actually a class definition, Okay? And it depends on the case, but most of the time, objects also start with capital letters. Even if it's an instance, because it's considered a type, more like a variable.

[00:10:42] We are going to use some of these options later, so we will come back to that.