This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll create our Card class using Kotlin!
Kotlin Links
Project Files
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've just finished creating our
Card class but we did it in Java.
0:00
Now let's do it in Kotlin.
0:05
Lucky for
0:07
us the nice folks over at IntelliJ
couldn't have made this any easier.
0:07
Just select the Code tab and
choose Convert Java File to Kotlin File
0:13
then hit OK and yeah Kotlin is awesome.
0:20
Not only did we just condense 27 lines
of Java and to one line of Kotlin but
0:25
we did it automatically and it wasn't
something special about the code we wrote,
0:31
you can convert pretty much any Java file
to Kotlin by using that same method.
0:36
So if you ever get stuck, remember you
can always write your code in Java and
0:42
then convert it to see
how it's done in Kotlin.
0:47
Nice, but as awesome as that was,
0:50
we did skip just a few steps by
having it convert automatically.
0:52
Let's undo that, and this time we'll
take things a little bit slower.
0:56
But first let's use
Cmd+Shift+Plus on Mac or
1:02
Ctrl+Shift+Plus on Windows to
expand the collapsed code sections.
1:06
Now, the first thing we need to talk about
in transitioning this Card class from Java
1:11
to Kotlin is the difference
between a field and a property.
1:16
We already know what a field is.
1:21
We've got three of them up here,
value, suit and faceUp.
1:23
But what's a property?
1:28
Well a property is just getters and
setters.
1:30
Here we have the Value property,
the Suit property And the FaceUp property.
1:34
Now looking at these examples,
1:39
you might be thinking that a property
always needs a field to back it up.
1:41
But that's not always the case.
1:46
For example, what if we want to
add a property called class name.
1:48
And we could do it something like this.
1:53
[SOUND] getClassName and
1:55
inside this function we could return
1:59
this.getClass.getSimpleName.
2:04
Now we have a class name property
without having a class name field.
2:10
Okay so sometimes we can have a property
without a field to support it.
2:15
Now that we know that let's get
rid of our class name property.
2:20
[SOUND].
2:23
And that's enough about properties for
now you ready for the big reveal?
2:26
All right here we go,
in Kotlin we can only declare properties.
2:30
Anytime we create a variable
in Kotlin it's a property.
2:36
Let's investigate this a bit more by
rewriting our card class in Kotlin.
2:40
Let's rename this class to JavaCard.
2:44
And then create a new
Kotlin class named Card.
2:54
And pick Class.
3:01
Then let's split JavaCard vertically,
to get it on the right.
3:04
And our new card class on the left.
3:08
Now give us a little
more room over here too.
3:15
And actually,
let's just hide the project name for now.
3:17
All right.
3:21
We can see that classes in Kotlin look
pretty much the same as classes in Java.
3:22
Let's start this party by
creating our value property.
3:27
In Kotlin, we start each property
declaration with either val or
3:31
var, depending on if
the property can change.
3:35
We use var for mutable properties.
3:39
And if it's an immutable property,
we use val.
3:42
Since the value of a card is immutable,
let's start with the val keyword and
3:45
then add the name of our property,
value, then let's add a colon and
3:50
after that,
let's type Int with a capital I.
3:55
Kotlin doesn't have primitives, so
we have to use the Int class but
3:59
we're still getting an error, Property
must be Initialized or be abstract.
4:04
Another thing about Kotlin is that
there aren't any default values.
4:09
And if a property doesn't have a value
it's an error, so on the next line let's
4:13
hit Tab and then type get,and pick
the option with the curly braces.
4:18
Then inside the curly braces,
let's type return 0, and that's it.
4:25
You've just created your
first Kotlin property.
4:32
And note that we didn't add a semi colon.
4:36
You can add semicolons if you like, but
Kotlin does just fine without them.
4:39
Okay, on the next line let's hit Tab, and
4:44
type set and, again,
pick the curly braces option.
4:48
And here we see exactly what
immutability means to Kotlin.
4:53
Since we declare this property as
immutable by using the val keyword,
4:56
Kotlin is not okay with
us adding a setter.
5:02
So, let's delete that.
5:05
Now another cool thing about Kotlin
is that if a function contains only
5:08
one expression, like this one does,
5:13
then we can rewrite it with
an equal sign like this.
5:15
However, an even cooler thing is that we
almost never need to specify the getters
5:26
and setters in this fashion.
5:30
We can just use the = 0 part and
Kotlin will fill in the rest.
5:32
All right,
that takes care of our value property.
5:37
Now let's handle or suit property.
5:40
And since the suit of
a card is also immutable,
5:42
we should start this property with right,
val.
5:45
So, val, suit and it's a String
5:49
and since we're required to give it
a value let's just use an empty string.
5:55
Nice.
6:00
Lastly, let's handle the face up property.
6:01
A card can change from
face up to face down,
6:04
meaning we'll start
this property with var.
6:07
Next comes the name of the property,
faceUp, followed by a colon,
6:11
and then the data type which
in this case is Boolean.
6:16
Finally, let's set it equal to false,
because each card should start face down.
6:21
Great.
6:27
We've got our card class, and
we've got our three properties.
6:27
In the next video,
6:30
we'll see how we can properly populate
these properties by adding a constructor.
6:32
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up