1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Classes, Objects, OOP, and RecyclerView for Android

Secondary Constructors

Start course
Overview
Difficulty
Intermediate
Duration
3h 12m
Students
1
Description

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.

Prerequisites

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

Transcript

Hello and welcome back. In this video, we'll continue our look at classes and explore the concept of secondary constructors. And secondary constructors are generally not used much, but you may come across them every now and then, so it's important to understand what they look like and what they do. And in our student example, let's say we wanted to implement the list of courses object associated with every student, which we briefly talked about before. 

Here in our print details method, we are printing out Empty for now. Let's say we wanted to implement this functionality. And what we'll do is use a mutable list, but we also want the ability to create students, so when we create a student object, we want the ability to create students with list of courses, or without a list of courses and add them later on. And one way to do this would be to have an empty list. So, right here, I'm going to start this off and say var listOfCourses, our limit list of courses, and I'll make this a mutable list. So, mutableListOf and I'll have the name of the course as a String. So, String. Right there. Okay. So, now I have an empty list of courses associated with every student object that's going to be created using this class. Now, what we want to do is have a way of initializing a student object, or creating a student object with a list of courses already when we create them. 

And that is where we're going to use a secondary constructor. So, right here, you see I have my first student, and this code will still work. So, instead of Enrolled in: Empty for now, if I reference this list of courses, listOfCourses, right there. And then if I run this, there we go. Enrolled in and I get an empty list. So, what I want to do is create another student right here. Student object. So, I'll say val studentTwo = Student("John", "Snow"). And I will give this a list of courses to initiate with. So, I'll call this sampleListOfCourses. And, of course, I have to define this because I haven't created it anyway. So, I'll define it right here. Say val sampleListOfCourses = mutableListOfString. And let's say "Computer Science", "Statistics", and "Psychology". We'll start with three. And you see an error showing up here. If I hover over it, too many arguments

because my constructor is expecting only two: first name and last name. So, let's go ahead and fix this by creating our second constructor. And the way you start it is by simply saying constructor and then { } and I'll put some code in here. And this is going to be very similar to how we created our first constructor right here, because remember we could have done this. We could have said constructor over here. And this code is the same as this, right? But before our primary constructor, we don't need to provide the constructor keyword unless we're going to modify it. But for our secondary constructor, we have to say constructor and then within here, we're going to use this first name and last name that our primary constructor is going to get. All right? So, firstName: String, okay? And then lastName: String as well. But the third item that we're going to use here is the list of courses, listOfCourses. And this is going to be of type MutableList and String. Right there.

So, now we have this constructor but it's not done. What we have to do then, you see, secondary constructors have to call the primary constructor for the first part, for this firstName and lastName. And then this listOfCourses, we're going to work on here. But for this first part, what we have to do is call the primary constructor. And we do that like this. We put a colon here and then we say "this", okay? Now, "this" refers to the object that's getting created and the instance that we're working in. So, this firstName, which means this one right here, firstName and lastName, okay? lastName. I'm going to move this to the next line, so you can see this better. All right. So, : this(firstName, lastName). That will take care of creating the object with the first name and last name that we got here. Now, for the listOfCourses, you see it's light gray because we haven't referenced it, and we're going to do that within this code block. { }. 

So, within this block, we're going to say this.listOfCourses, this again, because we're referring to the instance that's getting created, which started right here, you see this var listOfCourses. So, this listOfCourses = listOfCourses that I am passing in. There you go, and that's it. So, now I have a secondary constructor that actually uses this sample list of courses that I'm going to pass in and assign it as my list of courses property. So, if I run the code that I have right here, let me print out studentTwo as well, .printDetails. Then for studentOne, I'll get the same empty list, and for studentTwo, I'll get the list of courses that they're enrolled in. We'll actually put some extra code here. So, that we have some separation between the two. Let's put some dashes, and a new line. And this \n is a special character saying new line. So, we would get one new line after the dashes anyway. We'll move to the next line but then we'll have an extra line after that, okay? 

There's a special character in case you haven't come across anything like that before. So, 'Ctrl+Shift+R', let's run it. All right. There we go. I'm going to expand this a little bit. You see my first one right here, first name, last name enrolled in empty. And here, first name John Snow, full name, and there it is, enrolled in Computer Science, Statistics and Psychology. So, my first student used the primary constructor and my second student was able to use the secondary constructor in order to get the list of courses. And of course, you can add a list of saourses to the student as well. The first student for a studentOne, I can say studentOne.ListOfcourse, and I can assign the same list I have over here to studentOne as well or I can create a different one. 

For simplicity, I'll just assign studentOne sampleListOfCourses as well. Now I can use my print line method on studentOne.printDetails and it should say my studentOne the second time around we'll have this list of courses, so let's see. And there you go. The second time around. My student 1 now has a list of courses that they are enrolled in, the first time they were empty. So there it is, our introductory look at secondary constructors. Hope you found this useful and we'll leave it at that for this video. And in the next one, we'll jump into inheritance which is one of the key aspects of object oriented programming. Hope you're excited to learn about them, and 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