Abstract Classes
Start course
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 back. In this video, we'll take a quick look at abstract classes, and we'll be one step closer to wrapping up our look at object-oriented programming with Kotlin, and creating our own objects. And in the next video, we'll look at interfaces which will basically, complete it. Now, these are variations of classes but interestingly, you cannot create objects out of them; neither abstract classes nor interfaces. So, code like what we've been using in our main function over here, where we create objects; this will not work if InheritingStudent, let's say, was an abstract class. Okay, so why use abstract classes? Well, by definition, they're expected to be extended or basically, implemented in classes that use them. 

In the case of abstract classes, we use them to provide a common template for other classes that want to extend them. And any methods or properties declared as abstract, declared in abstract classes, behave as regular ones do, unless they're marked abstract as well. But if a member, let's say, a method is marked abstract, then it will have to be implemented or overwritten by the subclass. And that's a lot of information. Let's take a look at an example and get this going. I'm going to code out all the code that I had written before, so it doesn't interfere with anything we do here, and I will take my main function. Bring it up here. Okay, so we're ready to go. 

Let's define an abstract class, person or a human. I'm going to call it human, just to differentiate it from the older classes that we've worked with. And the way you do that is, say, abstract class, and then the name of the class. So, I'll say Human(). Okay. And we'll define var firstName of type String. And var lastName of type String as well. You see we have errors as I hover over them. You'll see that they're saying they must be initialized or be abstract. So the idea is, if you want to set this up as a template, or these variables up as a template, then all you have to do is call them abstract and then, you don't have to initialize them. But then, any class that inherits from this Human() class will need to initialize and use these two variables. But if I didn't use the word abstract, then I would have to initialize them. All right? And I'll also add a print details method as well, and leave it abstract. So, it'll need to be implemented by whoever extends this class. 

So, I'll call it abstract fun printDetails() Okay? And since again, I've called it abstract, I don't need to write the code for it. But then, just to show you that you can add other things here as well, I'm going to add a non abstract method for fullName that returns a String. So, fun fullName() and this will return a String. So, I have to specify that. return "$firstName, $lastName". Actually I'll say "Full name: $firstName, $lastName". Okay. So, here we have an abstract class that's usable with a couple of abstract member variables and a function and a method that's already been implemented. So, let's define two classes who I want to inherit from, or extend this abstract class Human(), and I'll call these Employee and Student. 

So, let's go ahead and create class Employee, and this will extend Human(). So, I have to do that. All right. And then class Student will also extend Human(). But notice that these are showing up in red and if I hover over it, you'll see that class Employee is not abstract and does not implement abstract base class member, and it provides the first name over here. Basically, what I need to do is implement these variables in my class which are marked abstract here. Anything marked abstract here, when I extend from it, will need to be implemented here. And you've seen us use something similar like this in the Youtube player, where we had to implement the member functions which were required for OnInitializedListener. So, I'll go here for Employee. See, there's 'More actions'. I'll click on that and you see this option to implement members, and it lists out all the ones that I need to implement. Okay? So, I'll highlight all of them. 

Okay. And our studio conveniently fills in all the code for us. We still have to implement these, so we have a few to do's but for now, I'll do the same for Student. So, hover over Student, 'More actions', 'Implement members'. Let's implement all three. All right. So, now we have templates ready to go for both Employee class and Student class, and we can pass in firstName and lastName as parameters, when creating objects of these classes. So, I'll move them to the constructor. So, I'll copy this 'Command + X, put this in the constructor, get rid of this code here, and then last name as well, 'Command + X' put this in the constructor as well. Get rid of this. Okay? And I'll do the same for Student. 'Command + X'. Okay. And I'll quickly fill in the printDetails method. 

All right, I'll paste some code in over here. Okay, so I have this println("Employee Details"), a line, and then First name, where I print out the $firstname, and Last name where I print out the $lastName. And here, I can use my full name member function from the human abstract class which I'm inheriting from. This will give me the full name. So, what I'll do here is simply call that function. So, println(fullName()), since this returns the fullName, I have to print it out in order for it to display it, but simply calling the function is good enough. And then, I'll add in some new lines. So, "\n\n" to get two new lines in there. Okay. And remember \n is special character signifying new lines. So, I'll get two new lines after this. All right, so that's the employee details. I'll go ahead and copy-paste this and add it to Student as well. So, 'Command + C' right here, 'Command + V', and here, instead of Employee Details, I'll say Student Details, and the rest should be okay. All right. 

Now, in my main function, I can go ahead and create objects of these types and run the printDetails method on them as well. So, down here, I'll say val mushrur = Student. I'll pass in first name and last name, and then I'll do mashrur.printDetails(). Similarly, I'll create john as an employee, John Doe, and then john.printDetails(). All right, so let's go ahead and run this. Make sure it works. And there you go. Okay. We have student details: Mashrur, Hossain, Mashrur Hossain. And then, employee details: John, Doe, John Doe. Perfect. And that's how you work with and build abstract classes, and then use them as templates if you want, in other classes. And that's a decent amount we've covered in this video. So, we're going to leave it at that, and in the next video, we'll look at interfaces and build a couple using this same example. I 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