‫Instructor: Hi, within this lecture,
‫we are going to see how
‫we can add a function inside of our class.
‫And also, we are 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 are going to have two classes right now.
‫The new class is called SuperMusician, okay?
‫So, this is kind of a superMusician compared
‫to our regular MusicianClass.
‫So, we are going to take all of this information
‫and we are going to use them in our superMusicianClass.
‫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 sync, for example.
‫So, this is not different
‫than creating any function inside of a class.
‫So, we have done this
‫a lot in view controllers, 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, the Nothing Else Matters, right?
‫So, let me run this.
‫And as you can see, now we get this printout over here.
‫So, what happens if I want to use this function
‫if I want to use all this 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 MusicianClass?
‫What if I'm not allowed to change anything
‫in the MusiciansClass?
‫Suppose that we are 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 objective-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 gonna come over here and say : Musicians.
‫So, it means that the
‫SuperMusician will inherit something from the Musicians.
‫It will get access the properties
‫and the functions of the MusiciansClass.
‫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 comment B,
‫okay?
‫And then let's go here and create Kirk.
‫So, SuperMusician, as you can see, if I open parenthesis
‫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 SuperMusicianClass.
‫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 lead guitar.
‫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'm 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 comment B so it'll 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 SuperMusicianClass.
‫So, SuperMusician can do everything a Musician can do
‫but also, he can sing2,
‫right?
‫He can do some extra future 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 MusiciansClass 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 MusiciansClass.
‫In this case, as you can see, super is a MusiciansClass.
‫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 this 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 are going to
‫learn about a new concept called Excess Levels.