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.
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 back. Carrying on from where we left off in the last video where we were able to create a couple of objects of our student class. What if we accidentally left out one of the properties that we're trying to create of the student?
For example, our student expects first name and last name, and here when I'm creating student, I'm providing first name, last name, what if I left out last name accidentally? Or let's say not accidentally. What if I just left this off? Then what would happen? Let me get rid of the second student for clarity. All right. So, if I run this, you see, I have a problem here. No value passed for parameter last name. So, since my class expects both first and last names to be provided and I failed to provide last name, we ran into this error. So, in this scenario, you might consider using or providing a default value in your class constructor. Let's say, in this case, I don't know the student's last name and I don't want that to stop me from creating a student object. Maybe I can add it in later. So, in that case, I can enter in a default value for the last name.
And the way you do that is after you declare var lastname: String, you can say equals and provide a default value. So, let's say I provide last name right here. So, now if I run this, Ctrl+Shift+R, I'll get rid of this. There you go, no more errors. And it says my first student is Mashrur and last name, last name is what I provided as the default last name. So, I can do the same for my first name here. String equals first name and then I don't even need to provide any foster last names and creating an object of my student class because you'll get initialized with first name of first name and last name value string of last name. If I run this, there you go. My first student, first name, last name. Obviously that's not very useful in this case but that's how you provide default values. Like let's say you wanted to initialize as John Doe then you would do that here. So, in our example so far we've added properties in our constructor and assigned values to them.
So, when I enter in some values it gets initialized and my object is created. But what if I wanted my constructor to do more? Let's go back to right here. Mashrur Hossain. So, I have first name Mashrur, last name Hossain when I create this object. But what if I wanted more? Then you can do that using init blocks. So, within my class, I can do this, init and you see this code helper pops up. So, init and then I have open and close curly brackets and within here I can do additional functions as we want. And you can have more than one init block, you can have as many as you want. So, let's say I want to print something out over here. I'll say println student object initializing. And I want this to print every time I'm creating an object and let's do another println here. I'll provide some details of the actual object that's getting created.
So, I'll say first name, last name created. And if I do this, I don't even need this println over here. If I run it right now, Ctrl+Shift+R. Again you'd want to check what that run command is on a PC and there you go. We get student object initializing. She's showing up from here and then Mashrur Hossain created, which is this init. So, obviously, we're simply doing println over here. But if you had more complex calculations or other things you wanted to do, you could do that here when an object is created. Or if you wanted other attributes or other calculations or anything else that you wanted to do when an object is created, you could do that here. Now something about these init blocks, they run in order. So, it's in the order that you specify over here and they all run when the constructor is called. And since they run in order, if you want to reference any properties in them or variables like I've done here, first name and last name, then you have to make sure that they're declared previously.
In my case right here, first name, last name are properties that are initialized here that's why I'm able to refer to them here. If I had done this in reverse order and first name and last name were not initialized when I came to this, it would throw an error. Now let's talk about methods and methods are also known as member functions. So, what do methods do? Usually, you would want your class to have some functionality. Your blueprints to be able to do something. For example, students can add courses, let's say, or something like that. In our case, we'll start out simple and we simply want to print the details of the student. I'll get rid of these init blocks over here. And let's say I want to add a method.
So, I'll call it fun, the same way we write regular methods or functions and I'll call it printDetails. And I want this member function or method to print out details of my student every time I run it. The idea is I would want to run it like this. When I create my student object, I'd want to say something like this, printDetails. So, when this is called, I want the details of the student to print out. So, I'll do something simple here. Println, first name, println, last name, println, full name. And I'll do an enrolled in, even though I don't have any courses list for the student as yet. I'll just leave it as empty for now. So, now I can call this method on any object. Any student object that I create. So, right here I'm creating a student and then I'm calling the printDetails method. This is similar to when we called methods on strings or other classes and objects that we've used before. We're doing something similar here.
So, let's run it, check it out. First name Mashrur. Last name, Hossain. Full name. Enrolled in empty for now. So, like this, you can have multiple methods or member functions and you can make your class to do a lot of things that you want. So, that's how you create member functions. Now, let's talk about getters and setters. Specifically, custom getters and setters. And we already discussed how by default for mutable property types here, we've used var. So, they're mutable property types. Kotlin automatically gives us getters and setters. And if they were immutable. For example, if this was val instead of var, then that would be an immutable data type. And then we'd only get getters for that property and not setter. So, we would be able to see what the first name is, but not set the first name after the object is created.
Basically, when the object is created, you assign first name and last name and that's it. So, what if I wanted a custom getter? Let's say I initiate a variable here or property. I'll say full name of type string and the first name and last name. And here full name instead of first name, last name, I will simply reference my property. Now I can access this property by simply going studentOne.fullName. And of course, this is if I only wanted to access this property. If I want to see all of it then I would simply use printDetails. But if I did this then run it. Whoops, I ran it without actually printing it out. I'm going to get rid of this first. Now I wanted to print out the property not just get it. Here we go. When we run this and there it is. You see I get the full name and it's Mashrur Hossain. So, I have my full name property. But see what happened, when I created the student object, I created it using all lowercase Mashrur Hossain.
What if I wanted full name to always display capitalized first and last name? So, capital M and capital H in this case. How would I do that? What I can do is customize the getter for this full name property and make sure the display of full name is always capitalized regardless of how it was written out at object creation time. So, how would we do that? We can write this method called get. I'm gonna tap this in so it's easier to understand that this get method is associated with this full name property. And you see this red line over here. If you hover over it, it's gonna say a return expression is required for this function. So, what I need to do is, I need to return the value that I want to be printed out when this property is called and you do that simply by using the return keyword. And what do I want returned?
Well, I want to construct first name and last name but capitalized. So, what I'll do is reference this, first name dot I'll run the capitalized method on it. There you go. And this will capitalize first name and then I'll do the same for last name. LastName.capitalize. Now what happens is when I write code like this, studentOne.fullName when I'm trying to access that property, doesn't matter that first name is lowercase Mashrur and last name is lowercase Hossain, it's gonna return capitalized versions of that. So, let's run it and make sure that works. There you go, Mashrur Hossain, capitalized. So, that's how you create custom getters.
Now, what if we wanted a custom setter? You do it like this, you type in set within parentheses value and then code block for whatever you want and the value here is whatever I send in. So, the way you use setters is let's say, I wanted to reset full name. What I would do is something like this, studentOne.fullName equals something. So, let's say I want John Snow to be the full name of student one. So, I'm trying to set full name to be John Snow, that means it'll come to this custom setter and this value will be equal to John Snow. So, then I can use value. But how do I reference full name? Well, I can reference full name within here by using this keyword field. So, field refers to the variable or the property who I'm trying to set, and value refers to what I'm passing in. Now you may ask why can't I use full name because that's gonna create a stack overflow over here. It's gonna get into a recursive infinite loop type situation where within full name, I'm trying to access full name. It's just a mess. So, the way you do that is by using field and then field I can set it to whatever I want.
So let's say you wanted to play a prank and if anyone ever tried to update full name like this, you wanna set full name to be Bilbo Baggins. So, if anyone ever tried to reset full name, it didn't matter what they tried to reset it to, you'd set it to Bilbo Baggins. Then you do it like this, actually to say that this is a joke I can say something like, "You wanted this to be value but I've set it to Bilbo Baggins." Just to be clear that this is a joke. And if I do this then my full name variable would be this whole string value right here. If I try to then print this out. If I did this as it stands right now, I would still get the correct Mashrur Hossain because if you look at this, I have a custom getter here that sets it to first name capitalized, last name capitalized no matter what I set the value of the field too. So, I'm going to temporarily code out the getter so we can see how the setter works.
All right, let's run it. There you go, first, we got the full name to print out as Mashrur Hossain and then I tried to reset full name to John Snow but then I got the custom setter to update it to this. So, you wanted this to be John Snow but I've set it to Bilbo Baggins. So, that's a custom setter for you. So, we've covered a very decent amount in this video. We learned how to set default values for properties if none are assigned during object creation. We also learned how to use initialization blocks so we can do more with our constructors if necessary and along with how to create member functions and to use custom getters and setters. So, we'll leave it at that in this video, and I'll see you in the next one, where we'll briefly take a look at secondary constructors. See you there.
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.