#210 Class Inheritance – Extends   Super (ES6 Completed)
Articles Blog

#210 Class Inheritance – Extends Super (ES6 Completed)


In this video you’re going to learn inheritance in E6 using extends and super. So let’s create a new class and then make that class inherits from our first class phone. 6 So let’s call this new class smartphone 6. So let’s give it a constructor which takes the make model an operating system and sets these says the properties using these values. All right. So now we want smartphone 6 to inherit everything from phone 6 no in iOS 5. You could do something like phone six dot call. So use the call method to specify the value of this which will be the instance of this class. And then you pass the promises you want to pass so that you run the constructor code of the superclass. All right. So if you run that code you have to do something like this. Right. And then you pass the parameters so in our case we’d have passed only the make and the model like that’s and we would have inherited the warranty color and everything else. Right. So everything else you find in the constructor of the superclass. So you could get rid of this leave only that line and use this in. Yes right now in iOS 6 you can. You can do this in a more simpler in a simpler way. So we will get rid of those two lines because we have called the make and model in the constructor of the superclass and we will use a function called super. So this function if you call it’s like that’s when you pass the make animal javascript. Now is going to look for the class or the superclass from which you are inheriting or trying to inherit properties and methods in the constructor. Now you need to specify what the superclass is. Okay. Because we haven’t said anywhere that we want to inherit from phone 6 or the class forensics. So you need to use the extents keyword like that’s followed by the name of the superclass. So if you try this then you have you inherit everything that you find in the constructor of the superclass basically you just running this code with this set to the instance of the new class or subclass. All right. Now let’s say that you of course also an extended warranty in this new cars and you want to use the same extend warranty of the superclass. So all you need to do is call super dots the extend the warranty of the superclass like that. This will work perfectly. All right. So let’s try this. So let’s create a new instance of smartphone so let’s give it a make model. The operating system terrorists log it’s. So this is what you get. So you can see the make model and also you can see the warranty color the log and so on. So this has been have been inherited. All right from the superclass. Now if you try extend the warranty by 10 and you logs again it works. So it’s jumped from 24 to 34 right now. If you check whether smartphone 6 is an instance of the new class obviously it is in or the new class because you created it using that class or as an instance of that class so you get true. Now if you try to check if it’s an instance of the superclass which is phone six you get also true because the subclass inherits everything from the superclass. So anything you create using smartphone 6 is also going to be an instance of forensics. All right. So hopefully this gave you a good understanding of inheritance between classes in iOS 6.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top