Classes in Kotlin are similar to those in Java, but with fewer restrictions. This video tutorial explains the similarities and differences.
- [Instructor] In Kotlin, you can define more than one class per file, unlike Java. By default classes are public which is the opposite of Java. In Kotlin the new keyword is not needed and enum is a soft-keyword. It only has significance when it's followed by the keyword class. Otherwise it is ignored. To create a class we're going to go over here to kotlin, we're going to right click on it, we're going to say New, Kotlin FileClass, in this case we're going to name it Person and say OK.
Delete that and we might as well include our package. Then we'll create our person class. Now the simplest class that you can create is just going to be person with an open and closed brace. A class can have one primary constructor and multiple secondary constructors. Let's add a primary constructor to our person class. So we'll say constructor, first name comma last name.
Most of the time we can omit the word constructor. It is only needed if the constructor has an annotation or a visibility modifier so let's remove it from ours. So we can just delete the word constructor. The primary constructor can't have any code. If your class needs to execute code in order to initialize itself, it can add an initializer block. So in this case we're going to type the word init, an open curly brace, then inside of it we're not really going to do anything useful, but we'll just print a message to let us know when we are creating this class and so we will say "Create a person named $firstName $lastName" and this'll just let us know that the class is being instantiated.
Now if we go back to our main we're going to new this class up so we will say that val me = Person, notice that you don't need the new, and we'll give it a first name, Troy comma last name, Miles and just to keep all that other stuff out of the file we're going to do a return here which basically will just comment out the code that we've been working on before so that our console is fairly neat and if we do a Control + Shift + R to run it we can see the message, Create a person named Troy Miles so that shows that our class got instantiated and me is currently equal to it.
Now if your class needs a visibility modifier or annotation, it must include the primary constructor. So let's go back to our person class and this time we'll make it internal. So if we say internal and internal, which is a visibility modifier, should go after the class name but before the class body. You notice that internal is going to give us the red squiggly line which says use constructor keyword after modifiers of primary constructor so we have to put the constructor name in there otherwise it won't compile.
So in this case we added an internal visibility modifier and had to add back the word constructor. If you need a secondary constructor you must call the primary constructor via this. So let's first off create a secondary constructor and you might need a secondary constructor for things like where you have constructors that take different number of parameters so our primary constructor might take the first name and last name, but our secondary constructor might take first name, last name, and middle name.
So we'll go ahead and create a new constructor and we'll give it a first name, a last name, a middle name, and as I said, we have to call the primary constructor and so we're going to say this and what we're going to pass.
In fact let's move this down one line. We're going to call this and we're going to go ahead and pass the first name and the last name to it. And then finally we'll actually create the body of our secondary constructor. So now if we go back into the main function we can say val you = Person and we will say Janet.
So we give the person a first name, a last name, and a middle name. And if we do a Control + Shift + R again to run this we see that we created a person named Troy Miles, a person named Janet Chung using different sets of parameters and that's how you create classes in Kotlin.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin