image
What is Inheritance?

Contents

Object-Oriented Programming
2
What is OOP?
PREVIEW6m 49s
3
11
Details
9m 33s

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
1h 22m
Students
25
Ratings
5/5
starstarstarstarstar
Description

This course delves into Object-Oriented Programming (OOP), covering its main components, and then putting these into practice by walking you through the creation of an app using OOP.

Learning Objectives

  • Learn about classes and how you can leverage them in your code.
  • Learn concepts such as Enum, Inheritance, and Access Levels
  • Understand how these can be used to build apps

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.

Prerequisites

To the most out of this course, you should have some basic understanding of programming and computer science in general.

 

Transcript

Hi, within this lecture, we're going to see how we can add a function inside of our class and also we're going to learn a very important object-oriented programming concept called inheritance. So, adding a function is fairly easy. So, I'm going to show that in a quick way, but before we do that I'm going to create another swift file because we're going to have two classes right now. The new class is called SuperMusician. Okay, so this is kind of a superior musician compared to our regular musician class. So, we're going to take all of this information and we're going to use them in our superior musician class, but right now I want to add a new function before we do that. So, adding a function is fairly easy as I said before, you just come into the class and you just say func sing, for example. So, this is not different than creating any function inside of a class. So, we have done this a lot in ViewControllers, right?

So, for example, if I make Kirk or James sing, they will sing, 'nothing else matters.' So, let me come over here and if I say james.sing, it will sing 'nothing else matters,' right? So, let me run this. And as you can see now we get this print out over here. So, what happens if I want to use this function? If I want to use all these properties inside my SuperMusician. I can copy and paste everything you know, and this would be okay, but it's not very efficient. What if I want to change something in musician class? What if I'm not allowed to change anything in the musicians class, suppose that we're working for a big company? So, it's not actually fair or it's not actually a good idea to copy and paste everything. Rather than that, I'm going to inherit this class.

Okay, so I can reach those values, reach those methods and functions and attributes. Can we do that? Yes, of course, we can do that because this is object-oriented programming, right? Right now, I don't even have a class in the SuperMusician, so I'm going to call the SuperMusician, and before we go into the class, I'm going to come over here and say : Musicians. So, it means that this SuperMusician will inherit something from the Musicians. It will get access to properties and the functions of the Musicians class. So, this is super cool. Okay, this is one of the greatest features of object-oriented programming. We can now create a SuperMusician object without doing anything actually.

So, let me create Kirk as a SuperMusician, okay? So, it doesn't see the SuperMusician, let me do command B, okay? And then let's go here and create Kirk. So, SuperMusician as you can see if I open parentheses, it will ask me for these parameters one more time. So, how does it work? We didn't even write these parameters inside of our SuperMusician class, we don't have anything in here. But since I'm inheriting from Musicians, I can use all of these properties. I can come over here and I can just say, this is going to be Kirk and the age will be 55 instrument guitar again, and type is going to be the LeadGuitar.

So, here we go, I have created Kirk, it's not a Musician, it's a SuperMusician, but I can even make him sing. So, let me delete all of these values in James so we can actually see what is Kirk singing. So, as you can see it says, nothing else matters. Even though I don't have that function in my class, it still works because I am leveraging inheritance. So, we can do other things as well. We can add some more features to SuperMusician. We can create a sing2 function for example, and we can just say another thing in here like enter, segment or enter night, okay? So, can I come over here, let me do a command B so it will get synchronized. Can I come over here and say Kirk.sing2? Of course, I can because Kirk is a SuperMusician and he can sing, he can even sing2. But can I come over here and say james.sing? Yes, I can because sing is in the Musicians, but as you can see, we cannot say james.sing2 because sing2 belongs to the SuperMusician class.

So, SuperMusician can do everything a Musician can do, but also he can sing2, right? He can do some extra feature, extra stuff. So, I can have another function in here. I can have another attributes and other functionalities as well, but we won't have them in Musicians, so that's the beauty of it. So, for example, let me show you what the override is. So, if I say override func sing, it means that sing is already defined for me, but I'm going to change it, I'm going to override it. If I write func sing, it will give me an error because this is already defined in the Musicians class and I'm inheriting from it.

So, it says that you have to override this. So, okay, I want to override this function, so I'm going to first call the original sing function, okay? And you can just say super. So, super refers to the inherited class. So, if you say super, it refers to Musicians class in this case, as you can see super is a Musicians class. So, I can reach Musicians with the keyword super in this inheriting class. So, I can just say super.sing, for example, I am just calling the super.sing function as usual so this will just print me 'nothing else matters.' But also I can add some new lines to 'nothing else matters,' right? I can just say 'exit light' for example. So, this will give me the 'nothing else matters' and it will give me 'exit light' as well.

So, let me go to Kirk and delete this sing2 or comment this out and I will only print out Kirk.sing, and as you can see, it printed out 'nothing else matters 'and it printed out 'exit light' because I have called super.sing. So, if I just do this with james.sing, you will only see nothing else matters because James cannot sing the other line. Because James is a Musician, but Kirk is a SuperMusician. So, that's inheritance, that's one of the most important aspects of object-oriented programming. And you can easily use this in your own codes as well. It is so easy to use and it is so convenient. So, let's stop here and within the next lecture, we're going to learn about a new concept called excess levels.

 

About the Author
Students
1706
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics