1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Object Oriented Programming (OOP) in Kotlin

Constructors - Part 1

Start course
Overview
Difficulty
Intermediate
Duration
1h 31m
Students
11
Ratings
5/5
starstarstarstarstar
Description

This course covers the concept of Object-Oriented Programming in Kotlin, which is a method of designing and implementing software. It simplifies software development and maintenance by providing concepts such as object, class, inheritance, polymorphism, abstraction, and encapsulation. This course will explore those.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Resources

Course GitHub repo: https://github.com/OakAcademy/Kotlin-Programming-Course/tree/main/ObjectOrientedProgramming

Transcript

Hello friends. So, in this video we're going to go through the constructor. Now, this is a very important topic of object oriented programming, because a constructor refers to the first method or function to be called when creating an object from any class. So, at least one constructor is required for each class. If you don't create any constructors, your compiler will create an empty constructor by default and who wants an empty constructor. So, now let's talk about constructors in Kotlin. The constructor structure in Kotlin can be a little different my friends. There are basically two different constructors in Kotlin. The first is the Primary Constructor and guess what the second one is? The Secondary Constructor. But the secondary constructor is similar to the constructor in Java. But the primary constructor is a little different. So, a class in Kotlin can have one primary or one or more secondary constructors. You follow? Just take a little look at the primary and secondary constructors now. The primary constructor is a part of the class header and it goes after the class name and optional type parameter.

If the primary constructor does not have any annotations or visibility modifiers, the constructor keyword can be omitted. The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, prefixed with the init keyword. Alright, so now, let's have a look at the secondary constructor. So, secondary constructors allow initialization of variables and allow you to provide some logic to the class as well. They are prefixed with a constructor keyword. And like I said earlier, the secondary constructor is quite similar to the constructor in Java. Alright, so, that's the theory and you've learned about class objects and constructors. So, really though, what are we doing here? Are we just learning the theory? No, let's get to it. We're going to put it into practice. So, go ahead, open up Android Studio. Now, we're going to create a new project. So, I'll select file, new, and new project. And these are all in the top left corner. Alright, so from this window that opens also like empty activity and press the next button. So, now we're going to continue and configure our project from this window.

So, I said the name of the project to be Object Oriented Programming. And I'm not going to change the package name and the file in which the project will be saved, so, the programming language will be Kotlin. Finally, the minimum SDK, let's call it 23. Now, let's create our project by clicking the finish button. Very cool. So, our project is being created and we shouldn't really take any action here. Just hang out for just a little bit until it's fully created. You want it to be perfect, no errors. And of course it depends on the performance of your computer. It might take a little while but hey, I want you to pay attention noted this, from the build tab up in here. You can see whether your project has been successfully created or not. So, what you're looking at here is yes, indeed our project is completely created. So, that means we can create a new Kotlin file. So, right click on the project package folder in the project directory right here. I'll select the new Kotlin class file option. And from here I'll just select the file option and we'll give a name to this file.

So, the name of this file can be object class constructor. Of course, you could write any name that you want to but okay, I'll leave it up to you. Anyway. We're going to create the Kotlin file by pressing the enter key on the keyboard. Now, we'll create a Kotlin class. And similarly, I'll just right click on the project package folder with the mouse. So, like new Kotlin class file. Now, this time I do have to select the class option and specify the name of class that I'm going to create. So well, but the name of this class be Cars. So, please note that the first letter of the class in the file name. You see that; it's uppercase. So, I created Kotlin class named Cars just by pressing enter and see how easy it is to create a class in Kotlin. Now again, I want you to notice that classes in Kotlin are expressed with the prefixed class. Excellent. So, now let's add a few properties to this class. For example, cars, what do they have, names and model? So, let's create variables that represent these properties. So, here I am writing var name: String. I'll right var model: Int on the bottom line. And of course, I don't want to assign any value to these variables at this initial stage.

But what do you notice? Android studio throws up a warning. It wants us to initialize these variables. Well, who you going to call? I mean, well, never mind. We going to trust. In any event, if you do remember back some previous lessons ago when we created any variable inside the function we could not initialize this variable. So, now  that we could pass values to variables later on but in Kotlin we absolutely need to initialize the variables that you create in a class. So, now there's a couple of different solutions at this stage. So, first we can assign an empty value for string variables. So, if we just put double quotes on the right side of the equal side, we actually initialize it but pass in a no value. Or if we write zero on the right side of the equals for energy variables, we're still going to initialize the variable. We can then pass the values that we want to pass to these variables. So, that is the first method. But see, in this method we still assigned a value to the variables. So, let's have a look at the second method.

Now, in the second method, after the string or integer expression here, we can put a question mark and write null on the right side of the equal sign. So, this method is called Nullable in Kotlin. In fact, it's thanks to this method, the null errors that we frequently encounter in Java are prevented. Now, because we can define variables without assigning any variable, we prevent the program for throwing up any errors, even if we forget to transfer value to the variables later. Now, you will understand why that's important better with time but for now, it's enough to know that we'll just write it this way. And we'll be able to define variables in the class without initializing. All right. So, thus we have now determined two different properties of the class named cars. Now, let's return the Kotlin file we just created. So, from this file we can access variables or properties in the class named cars. So, for this we need to create objects from the class called cars. Of course we need to do the object creation process and the main method because Kotlin needs at least one main method. So, I'll type maina here and press enter. And as you can see, the main method has been created so we can go ahead and create an object of the class, cars. So for this, I've got to write var here and specify a name for the object that I will create. I'm writing my car. And of course you could give it any name you want to. And notice too that the variable consists of two words. The first letter of the first word is lower case, the first letter, the second word is uppercase, right. That's the camel rule. So, after determining the name of the object that we're going to create, we just need to put the equal sign and write the name of the class from which we will create the object. So, I'm just writing in cars here. Now, did you notice the cars class suggested by the code editor is the cars class and the package we created. So, if we select this and put the princess at the end, we will create an object of the class named cards. So, now using this my car object, we can easily access the features and functions in the car's class if there are any.

So, for this we'll put a dot after typing my car here. So, after putting a dot, the features and the cars class and the functions if any will be presented to us by the editor. All right, so, that way we can use that feature, you just thought it was magic. So, first I'll select the name property from here and I'll give a name to the car, so, let's call it Ferrari. Now, let's determine the model of the car in a similar ways so, I'm writing the myCar.model. And we're going to go with the model to be 2021. So, we'll just keep it by year, model year 2021. So, now we're going to print the name and the model of the car on the console. I'll type sout, press enter, I'll write my cars name is myCar.name and it's model is myCar.model and parenthesis. All right, so, now let's run our code. So, what you're looking at here is, we can see the name and the model of the car on the console screen. But the point that you really do need to pay attention to is this; we have defined any variable named name or model in the main method.

but thanks to the object we created from the cars class, we have assigned the cars class and we have passed a value to the properties named name and model, that are defined in that class. You follow? So, we were able to display these values in the console because of that. So, that is the basis of Object Oriented Programming, my friends. So, if you get the concept, you're not going to have any difficulty at all when you're developing applications or even learning different programming language. So, if you did trip up a little bit, that's okay. Just go back and review and pretty soon, it's not going to be a problem at all. So, here we have created only one object from the class named cars. Of course, you can create as many objects as you want. So, for an example, I'm just going to create one more object and then we'll just call it quits here. So, I'll write var myCar2 = cars. This time the name of the car is Mercedes. And here I'll write, myCar2.name = Mercedes. So, now let's define the model. So, I'm typing myCar2.model = 2010.

Now, let's print this to the console. So, I'm typing here the first expression. I'm copying this and pasting it on bottom line. I'm typing the second here and I'll organize these objects as myCar2. Cool? So, now let's run the code. All right. So, as you can see, we see the name and the model of the first and second car in the console. So, that way you can create as many objects as you want and then transfer different values. All right. So, we'll break right here. and then we're going to practice our constructors in the next, well, the next time we meet in the next video all right, so, see you on the other side.

 

About the Author
Students
85
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics