Welcome back and this video, I would like to talk about a specific type of a class, which is a data
class, so let's create a class real quick in order to create a Delta class.
You can just go ahead and use the keyword data and then you'll see that the class.
So it's inside of the class here, codling basics.
That's at least how it's going to be called directly.
But I want to call this one user.
And in here I have to at least add one parameter.
And I'm going to add, for example, the ID, which is going to be a long ID and a name, which is going
to be a string.
Now I can use to start a class in order to create objects of it, so let's do that real quick here user
and it's going to be a user one or that is going to be one and the name is going to be doneness.
So that's already a user object.
Of course, we can call this one user one in order to be more precise.
So that's how we create an object of a class.
Now, when creating the other classes, there are things that you need to consider.
So the primary constructor must have at least one parameter.
So you can see if I get rid of that, then we get an error here because we don't have any parameter
in there.
But as I said, one is the minimum.
Then you need to assign them either as var or value.
So both is fine and other classes cannot be abstract, open, sealed or in other classes.
So none of these things work.
So you can go ahead and use open here in front of it.
So as you can see, it's not going to accept that.
In order to now access a property, we can simply go ahead and use it like this, we can just say user
one name, for example, and that will give us the name in this case, it will give us Dennis so we
can, of course, check that print line name.
So let's check it.
And that we are it says then.
OK, so that's how you can do it now, let's say you want to assign a new value, so instead of having
the I.D., one you want to assign the value to.
Well, the problem is that won't work because it's a volle, right?
So when you hover over it, Volle cannot be reassigned and you shouldn't redesign an ID, but maybe
you want to be able to reassign the name.
So you want to change the name of a specific user, then you can of course do so and replace that with,
for example, Michael.
All right.
So that's going to be now Michael, user one is not going to be Dennis anymore, but it's going to be
Michael now.
And then, of course, you can create new users any time.
And you can also compare them, so let's say we have yuzu to.
So will create a new user here of all user two is going to be a user object, which I give the ID of
one and also the name of Michael.
Right.
Because what I want to show you is that you can compare them so you can go ahead and compare user one
equals.
User.
All right, so because every object is of type, any at one point, so inherits from any.
That's why we can use the equals function here and alternatively, you can replace it with a double
equals sign.
So both versions are fine and this will be true.
We can, of course, test that and see if that's actually the case.
And you are that's true because both have the same content, right?
So in both cases, the user I.D. one and the name is Michael.
OK, so of course, here we can follow the instruction and just use a double equals sign, which will
result in two as well.
The classes also have a two string method, so the same as other classes as well, so user details Kolon
and the end user.
So just using this is going to work totally fine because our data class has a two string method underlying
it, because every class is inheriting any at one point, and that's why it's available in there as
well.
Another cool function is the copy function.
So if you want to copy one object to another, you can do that easily.
So let's say we have this updated user object, which will be of type user.
And here I'm just saying, OK, I want to copy user one so you can just go ahead and do it like this
or you can copy its contents except for the variables that you want to change.
So let's say I want to change that name to Dennis Pontotoc.
So user one will still be Michael, but now our updated user will be Dennis Puto, but will also have
the idea of one, even though it is usually should be unique.
But I think in this case it's going to be fine.
So let's print our updated user and let's see what we get there.
And by the way, we should maybe also print our user one just to see the difference between the two.
All right, so there we are.
It says it from the top there then we had true this was this comparison that we had and then user details.
That's this one here where it says user I.D., one name, Michael.
And then we have one user, which is our user, one which has the idea of one and the name of Michael
and the other user, which is our updated user, which has the same ID and a different name.
Now, in this case, it might be redundant.
It might seem stupid to do that, but not all data classes are as basic as we have it here with just
two variables or two parameters.
Imagine a data class with, let's say, seven different variables or parameters here.
Then it's already a very different story because then it might really make sense to just change one
thing and then copy the rest.
OK, so let's look at our updated user.
The updated user has an ID and a name.
Now what we can do is we can get its components so we can go ahead and.
Get component number one, so user 1.0 in this case was updated user.
That component one will give us the first component, so to speak, this one will print edition and
then print line updates its user component to will print.
Benazir Bhutto.
OK, so this is the.
Feature that you can use, another cool thing is that we can use deconstruction, so let's look at deconstruction.
So I have this I.D. and the name variable.
So I have these two variables that I want to make dependent on our updated user.
So what will happen now is that is going to take the ID and the name from updated user and it's going
to store them in separate variables.
So it's the same thing as going ahead and saying ID is equal to update its user ID.
And while I your name is updated user name now, of course, in our case, it's complaining because
name already existed up here.
So let's get rid of this.
And now.
We can use this functionality, so now we can go ahead and create variables out of the different attributes
that are within an object and I can, of course, go ahead and print out.
So ID is going to be ID and name is going to be.
Name.
All right, so.
This will print I.D. is one and name is going to be deficit neutral, so let's test that.
And we are so it says it one and name is Dennis one, you know.
All right.
So this was just a little introduction into later classes.
And we're going to use data classes because there's just very powerful, very basic classes, which
are very limited in their functionality.
But that is exactly what we need.
Sometimes we just don't want to have all of this overhead and all of this additional code, and we just
want to have a clean and simple object type.
And therefore, the classes are amazing.
All right.
So see you in the next video.