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

Inheritance

Start course
Overview
Difficulty
Intermediate
Duration
3h 12m
Students
2
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 start talking about inheritance. Inheritance is a concept where you have a class, and then there are other classes known as sub-classes, which can inherit all the characteristics of that class. So, take our Student class, for example; you could have a student athlete class that could inherit all the properties and methods of the student class and implement them. But maybe have some additional features that you would want your student athletes to have. Like, on top of their list of courses, they could also have some sport that they're interested in. And this is better done with a demo. So, let's start with a person. A person will have a first name and last name as well. 

So, I'll copy this constructor from the student and create a person class. You can do it right here on the same file. You can have multiple classes in the same file. So, I'll say class Person. I'll get rid of this notification. Alright. And I'll take this constructor from my student class. Command C. So, our person's going to have first name and last name as properties, and just like students, my person could have full name as well. So, I'll copy this one as well, okay? So, I'll have a var fullName, which is a member variable. That's going to give me first name and last name in a string. And I could have a printDetails() method as well for a person, right? It would simply list out first name, last name, and full name. And since a person by definition, won't have a list of courses. I'll skip that. So, I'll copy this printDetails() method and I'll paste it in my person class as well, okay? And I'll get rid of this list of courses and the new line. I don't think I need that. 

Alright. So, now I have a working person class with some properties and a method. So, let's run it and make sure I can create person objects and use them. So, let's scroll down to the bottom to my main. I'll leave the sample list of courses there for now. I'll get rid of everything else. Actually, I'll just quote it out because I don't need it right now. So, here I'll create a person. I'll call him john = Person and I'll initiate 'John' with first name of 'John' last name of 'Doe'. And then I can then call the printDetails() method right there. Let's run it and make sure it works. There you go. First name, last name, full name, and it's working. So good. And to minimize this, okay? Now back to our Person class, let's add a couple of methods to person. Generally, you would think a person could walk or run, right? Those are things that people tend to do. So, right here I will add a method, call it run(). And all it'll do is print the name of the person, so full name is running. Oops, I made a mistake there. It's full name with  a capital N. Okay. and then a fun walk()

This will print "fullName is walking", okay? So, I have a couple of member functions or methods for my person class, which means I can make John run or walk. So, right here, let's say john.walk() and john.run(). Okay. If I run this, there you go. It's working as expected. So, how do we apply this to inheritance or why are we building this up? So, we have this student class that we created. For a second forget that we already built this student class, okay? Imagine that it doesn't exist. So, as a programmer, if you're thinking of building a student class, now that you already have a person class, we know that person already has first name, already has last name, already has printDetails run() and walk(). And these are all things that a student is likely going to be able to use. 

So, instead of writing all the code again from scratch. Like printDetails() and having all this from scratch, in designing student, we can simply inherit from person. So, we have a working student already and modified as we need. So, to separate this class that I'm going to be building right now that inherits from person. I'll call it Inheriting Student instead of just a student, okay? So, I won't get rid of Student for reference. So, I'll create a class and I'll call it class InheritingStudent and remember class names are camel case, so each word within it will have to be capitalized and to indicate that I want InheritingStudents to inherit from Person, I have to use this colon, alright? So, InheritingStudent : and then who I want to inherit from which is Person(), but I can't just say Person, I have to put parentheses like this, all right? And then open close curly braces but you see I have an error over here. Why do I have an error? Let's hover over it. It says this type is final. 

So, it cannot be inherited from. So, that's something with Kotlin. When you create a class, it's not automatically inheritable, okay? These classes are created as final. So, if you want to make them something that other classes can inherit from. You have to put a modifier over here, and use this open keyword, all right? So, open then class Person. And now you see this error is gone from here. So, just with this code alone because we have default values for first name and last name in our Person constructor. We can actually create an object of this class InheritingStudent. Let's check it out. I'll get rid of John from here. Actually, I won't get rid of John. I will make John an InheritingStudent right there, okay? And my InheritingStudent cannot accept any parameters for first name, last name. So, I'm going to get rid of first name, last name for now. So, that the error disappears, okay? See now I have no errors. So, if I run this, check it out. It works. 

First name, first name, last name, last name, full name, first name, last name. And then the full name is walking and running. We didn't write any code here. In InheritingStudent, beyond saying it inherits from Person, but we are able to get all this functionality. Now, obviously, we are seeing first name and last name here. These are the default values for my person constructor, and if I didn't have default values provided over here, it wouldn't have worked. But obviously, this is not very useful because I would want to have a first name and last name that I want to provide for the objects or the student that I'm creating. So, if I bring back John Doe as first name, last name, then you see I get this error over here. And this red, it says that there are too many arguments. That's because our InheritingStudent doesn't have any parameters for first name and last name. So, let's go ahead and add that ability in InheritingStudent right here. 

Actually, I'll create some space. So, we don't have to look at the student class. So, right here, I need to give InheritingStudent the ability to accept first name and last name as parameters. So, I'll say firstName of type String, and then lastName of type String. Now you see if I scroll down the red from here is gone. But if I run this now. Unfortunately, we still get the same default values for first name and last name, and that's how we're constructing our object. So, what happened here? Why are we getting this? Let's look at our class again. Right here , you see, while I'm providing first name and last name, I'm not really doing anything with these parameters. Since, all I'm doing is inheriting from Person and you see I'm calling the constructor for  a Person over here. I will need to pass this first name and last name into the constructor for a person. So a person doesn't use its default first name and last name values, okay? And the way to do that is simply firstName and lastName, okay? So, you see what's happening, I'm calling my constructor Person now with the first name and last name that I'm getting here. 

And if you want to be more explicit, you can also say, firstName = firstName and then lastName = lastName. Yo can do this too. Let's run it and see if it works. Control shift R, there we go. Now I have 'john' as a Student or InheritingStudent object to be more precise, walking and running all over the place with the correct details. Great. But I'm going to leave these out because this was just to show you that there's a way you can do that. But I personally don't like doing that for this example, okay? So, now we're in a position to see code like this and compare it to our main activity of any of the apps that we've been working in. So, I'll pull up the main activity of this app. And you see here, class MainActivity, the class we're creating for our app. It inherits from AppCompactActivity() and you see the parentheses here. And that's the basic concept of inheritance for you in a nutshell. In this video, we've learned how to create a class InheritingStudent which inherits from a Parent class which is person and all its default characteristics and methods. But notice in the main activity there are a few more things. 

You see there is this override over here, before the function onCreate. So, on create being a member function or a method of MainActivity has this override keyword. And then within the method you have this super keyword. So, what are these modifiers or methods or features or keywords? What do these do? These are the types of things that you work with when you want to extend the abilities that your parent class give you. So, in our case in the MainActivity we want to extend what AppCompatActivity() gives us and build on top of it. So, you start with all the basics inherited from AppCompatActivity() or the Parent, and you're able to take it further. An that's what we're going to cover in the next video. Hope you're excited. And I'll see you there.

 

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