Intro to OOP with Kotlin
Intro to OOP with Kotlin
3h 12m

The course covers Recycler Views, which is one of the most important features of Android app development. They allow us to work with lists or grids of data with a ton of built-in optimizations. We'll then put your knowledge into practice by building an app to display a list of posts that we can scroll through, edit them, delete them, etc.

We'll also explore object-oriented programming with Kotlin, focusing on how to build objects, work with inheritance concepts and interfaces, and other highly advanced features. You'll also learn about classes and objects and how to work with them with the Kotlin programming language.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.


Since this is a beginner level course, there are no requirements, but any previous experience with coding would be beneficial.


Hello, and welcome to the section. As mentioned in the intro, we'll end up working with Recycler Views. But at this point, it's very important to dive a bit deeper into objects and having the ability to create our own objects. So, for this work, we're going to be working with Kotlin, the language directly and just running so many programs. So, if you want, you can use the Android studio, which we've been using so far, and that's what I'm going to be using and I'll show you how to do that, or you can also check out a very good integrated development environment, it's called IntelliJ IDEA and it's built to by JetBrains the people who created the Kotlin programming language. So, if you pull up a browser tab, here we go. 

And if you look for IntelliJ IDEA, there you go. Feel free to use this as well. There's a free version and a paid version. It's very easy to use and quite good. But since we are already using Android studio and we have it set up and we're fairly familiar with it, I'm just going to keep using that, okay? So, back to the studio. What we're going to do is create a Kotlin file that we can run. So, over here under app, so Android, app, java and com.examplekotlindemo, which is the package for the android app or any android app that we've been creating. And underneath there, we have a MainActivity, and that is our MainActivity.kt file. 

So, here you can right-click on this folder and create a new Kotlin file or Java class or a lot of other things you can do from here. I'm going to create a new folder or package within this folder. So, I'll keep all my demo code over there. So, right-click and then 'New'. And you see here that you can create a package. So, com.example.kotlindemo. I'm going to call it classesdemo, and there it is, classesdemo. Now, right-click 'New' and 'Kotlin file' and I'm going to call this Classes. 'Classes'. There you go. So, 'classes.kt' is the file name that got created. I'm going to make some space over here, okay? So, here I can write Kotlin code. So, I'll start with my main() function. So, fun main(). And let's test out println("Hello World"). And to run this, you see this a Run button over here or in a Mac,

it's control shift R. So, if you click on it, you see right there control shift R. So, it could be different in your Windows machine. Just take a look at what it is. So, that's the shortcut to run it. I'm just going to use the shortcut. So, control shift R. And the first time since this is a new project, it might take a while to build but each run after that, as long as it's just this, the compilation is pretty quick, and this is where the IntelliJ IDEA IDE might be a little bit faster, but again, this is only for the first run. There we go. A console pops up at the bottom and you see 'Hello World' is printed. Great. Now, if I try to print something else, control shift R. You see how that was significantly quicker, okay? So, now we have the interface to write our code and get some output down at the bottom. 

So, let's talk about classes which are basically the blueprint for creating objects, and you'll come across the 'Class' keyword in pretty much every programming language. I'm going to get rid of this. So, every programming language you're going to come across classes. Why do we need them? Well, you need to represent more complex things than just strings and integers, right? We've been working with strings and integers and the like, when we've been doing things or creating things ourselves, but within Android studio itself, check out all this stuff 'class MainActivity'. Then we've been working with views, right? Then we've been working with all these attributes. How are all these things represented? These are all done using classes. Basically, they are a way to represent anything custom that you would want to create and work with in a programming language. 

So, let's take a look at some examples. Think about an aquarium. An aquarium will have length, width then height as its dimensions. And if you were to represent it digitally, you would declare these as properties probably and create a representation of an aquarium, and then you can change these things and change the size of your aquarium. Think of Mcdonald's chain. Mcdonald's restaurant. You'd want some kind of blueprint that you can use and then use that same blueprint to replicate and create multiple locations of Mcdonald's restaurants. So, multiple objects. Think of a car blueprint. You'd have the make, model. So, make, model, whether the car is electric or gas and so on as the properties, and then you could create multiple cars from that car blueprint. More along the lines of computers or internet, think of a blog post, right? You'd have a title in a blog post, a body, author. 

So, you need a way to represent all of these. How about a person? You'd have a first name, last name and so on. So, all of these things. These are complex. These are more complex than simply numbers or strings. So, in order to work with them, you need to create them, and to create multiple versions of them or different people, different blog posts, multiple cars, you need a blueprint that you can use again and again. And within each blueprint you'd also have methods not just properties, right? For example, a car it can accelerate. So, this would be  a method or also known as a member function and it's associated with the car class. Similarly, a person let's say can run or can eat or can program. So, these three then would be examples of methods associated with the person. So, method or member function and you'd include them within the Person class. 

So, you see how you can slowly build this out and make classes do more and more and eventually end up with things like these, where you have an entire studio setup and you are able to build apps simply by using classes that have been defined and making slight modifications to them. All right. So, let's see this in action. So, how do we create a class? We start with the 'Class' keyword and I will start off with creating a Student class, right? So, a blueprint for a student class. And a student will have first name, last name. Let's keep it simple and start with that. So, at the very least to have a class, you start with the 'class' keyword and then the name of the class, and the name of the class has to be camel case, upper camel case. 

So, the first letter will have to be capitalized as well. So, if you had Student Athlete class for example, it would be StudentAthlete like that. So, upper camel case. And that's it. At a minimum to have a class, you just have the 'Class' keyword and the name of the class, and then we can create an Object of that class. So, we can initiate or instantiate an object, and we can call it, let's say 'val studentOne' and to create a new object of the class In Kotlin, you simply reference the class name followed by parentheses, okay? So, for people coming from other programming languages, you don't have to use the new keyword. So, once we do this, if we run it, we'll have a student object and to prove it, let's print our student object out. 

So, studentOne, the one that we just created. Let's print it out. Let's see what happens. So, control shift R. I don't know what happened there. If I rerun it, it's working. There you go. So, I don't know what happened first time, I probably had a compilation error of some kind. But anyway, you can see that I have this output over here and that's just some gibberish, right? It doesn't really do anything or say anything or give me anything useful. It's just an output of the object, right? You see it's classes there student followed by some other digits. So, anyway, you can see that an object of class Student was created. Let's go ahead and add first name and last name. And the simplest way I can think of doing that, right? You would put a block So, open curly brace, close curly brace and within it, let's go ahead and say 'var firstName' and I'll set it to 'Mashrur', and then 'var lastName' and I'll set it to my lastName, and over here now I can access these variables that are within my student class. 

Basically what Kotlin does is it gives me getters and setters to access these properties just by having them declared like this. So, let's get the getter meaning let's get the value of first name. So, I'll print it out, studentOne.firstName, can also println(studentOne.lastName). 'Ctrl' 'Shift' 'R'. Let's run it. And there you go. Mashrur Hossain. I'm able to access both of these properties of this class. And because I've used var over here, which is mutable, I can actually change the value of first name or last name. For example, here, I can say, "studentOne.firstName" And let's set it to "John". Okay. Now, if I print them out again. Command C. And then... Here we go. At the bottom here, I should see, it should be "Mashrur Hossain" then "John Hossain". 

And there you go. Mashrur Hossain and then John Hossain. Because the first name of my studentOne object has been changed over here. But notice an issue here with the way we're doing it. I'm hard coding the name over here, and then if I need to change it, I'm changing it here. I can also set these from here. But why would I want to do that, right? I wouldn't want my class to have the properties already defined. I want to create multiple students with multiple names, which means I would want the properties to be filled in, so first name and last name values, when I create the student object, right? Right here. So, if I wanted to create a second student, I could do that and so on. 

And for that, we can use a constructor. And in Kotlin, it's super concise. All you have to do is put parentheses after your class name. And within it, you just add in the properties that you want to initialize. So, I'll say, "var firstName" I have to declare what type I'm going to expect for it, which is "String", right there. And then "var lastName" of type String as well. And it's showing me errors here because I have them as var, and then I'm declaring the var again below it. I can't do that, obviously. I don't even need this anymore. And there you go. So, now I have first name property, last name property that I can create when I'm creating an object of the class Student. So, right here, studentOne instead of all this, I'll get rid of all this. Let's go ahead and add in first name right here,  "Mashrur". And then last name, "Hossain". Now, I can println(studentOne.firstName) println(studentOne.lastName) Okay. So, at this point, you see the names are getting initialized when the Student object is getting created. So, if I run this... Check it out. 

Now, because of this mechanism that I have in place, I can create multiple Student objects. So, instead of this, I can say something like this, println("My first student") And then let's output the students' first and last names. So, I'll use a template string here, studentOne.firstName and studentOne.lastName Then I can go ahead and create a second Student object, val studentTwo This is going to be Student as well. And here, see? In the first student creation process, I specified that I want the first name property to be Mashrur, and I want the last name property to  be Hossain. Whoops. I spelled my name wrong. That's interesting. Okay. But if you know the order that the class expects, and you know how many arguments or how many properties the class expects, then you actually don't need to specify the first name or last name here. 

So, let's create John Doe in that way. I know I need first name and last name to create an object. So, I'll simply say, "John" and then "Doe". And what Kotlin will do is take John to be the first one, right here, first name property, and Doe to be the last name property. And then I can print this out. And I'll say, "My second student". And here, "studentTwo.firstName" There you go. Then if I run this... And there it is, my first student, Mashrur Hossain. My second student, John Doe. All right. So, we've successfully written our first class and created a couple of objects of that class, and did something with those objects. In this case, we just printed out first name, last name. And we've covered a decent amount in this video, so we're going to leave it at that. And in the next video, we'll keep building out our class and adding in some methods to the class so it can perform some functions for us. All right. Hope you're excited. I'll see you in the next video.


About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics