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 data class real quick.
In order to create a data 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, Cortland 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 the idea is going to be one and the name is going to be Dennis.
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 data 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 volume.
So both the swine and other classes cannot be abstract, open, sealed or in their classes.
So none of these things work.
So you cannot 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, you know, acts as a property, we can simply go ahead and use it like this, we can just
say user one dog 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 there we are it says, Dennis.
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 vowel.
Right?
So when you hover over it, wall cannot be reassigned and you shouldn't reassign 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 User1 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 user to.
So it would create a new user here of all user to is going to be a user object, which I give the idea
of one and also the name of Michael Wright, because what I want to show you is that you can compare
them so you can go ahead and compare user one equals.
User two.
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 print.
True.
We can, of course, test that and see if that's actually the case.
And there you are.
That's true, because both have the same content, right?
So in both cases, the user ID is one and the name is Michael.
OK, so of course, here we can follow the instruction and just use a double equal sign, which will
result in true as well.
Other classes also have a two string method, so the same as other classes as well, so user details
colon and a user.
So just using this as going to work totally fine because our data class has a two string method underlying
it because every class is inheriting any and 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 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 Potuto.
So user one will still be Michael, but now our updated user will be Dennis, one user, but will also
have the idea of one, even though it 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 then, is it from the top there than we had through 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.
Good component, number one.
So, user one dot or in this case, it was updated user.
That component one will give us the first component, so to speak, this one will print I.D. one and
then print line updated user component two will print.
Then this you to.
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 idea 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 it is equal to updated user dot ID.
And while I know 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, all of the different attributes
that are within an object.
And I can, of course, go ahead and print those.
So ID is going to be ID and name is going to be.
Name.
All right, so.
This will print, Heidi, is one name is going to be Dennis Produto.
So let's test it.
And there we are, so it says IDs, one and name is then upon you, though.
All right.
So this was just a little introduction into beta 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 data classes are amazing.
All right, so see you in the next video.