Swift - What is the "Final" keyword?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're talking about the final keyword in swift we're going to talk about the two main benefits one is preventing inheritance and two is the performance gains well we'll dive into that a little bit later before we talk about blocking inheritance let me give a quick 10 second overview on what inheritance is and a great example here is a custom button that i've created a gf button it looks like this as you can see you know you can set the background color it's got a nice bold font rounded edges that's the custom button so what it does here is you can see it inherits from ui button so what that means is my gf button gets all the bells and whistles that ui button has that apple built however i can just kind of put my nice little code of paint my little extra features on that button i can build on top of ui button and i can build on top of it because i'm inheriting all the stuff from ui button okay so that's inheritance in a nutshell so what the final keyword does is it blocks inheritance let me show you so i have my gf button here if i scroll down let's say i wanted to create a class gf button junior we're inheriting from this now so this would inherit from gf button cool and then i could do my class stuff down there this is fine i'm allowed to because i haven't marked gf button as final but now if i mark it as final now you're going to see and i build here you see i'm going to get an error down here build failed why did it fail inheritance from a class g final class gf button so again i've blocked the inheritance past gfbutten i said you cannot inherit from this class now you may be wondering like why would i want to do that well let's say you're building like an sdk or a framework something that other developers are going to import into their code base to use let's take stripe for example stripe is a very common sdk to allow you to you know take credit card payments and accept payments and all that just keeping it real simple well if you're stripe you've built this whole you know sdk you don't want developers coming in overriding your classes and putting their own behavior on it right you built everything to work together the way you intended not only can they do their own customizations but it could potentially be abused so as an sdk developer like you don't want that right so you want to mark your classes and stuff uh as final and that prevents that inheritance uh building on this before we move on to the performance gains so right now you see i have my classes final well let's say i don't want to block the entire class from being inherited however there are certain behaviors on this class that i don't want users of my sdk or framework or api i don't want them to override that specific function because when you subclass like right now because i got rid of final my gf button junior is fine let's take this example spoiler alert your i forgot to delete this from my little practice run a little little behind the scenes right there spoiler alert you can set individual functions uh as final so i deleted that final so now you can see i can override and then set uh background color and i can put in my own behaviors right i'm overriding this function however if i put the final in there again i spoiled it and do a command b you're going to see i get the error that says instant method override over has a final instance method so i can't override it so again to sum that up you can either put final in front of the entire class and that can't be inherited or if you just want to block off you know specific methods you can put final in front of that function signature and that will block that from being overridden all right so let's delete gfbutton jr since we are done messing with that and let's talk about the performance gains of using final i'm not going to pretend like i'm a compiler engineer i don't really know much about this stuff so let's go to the documentation real quick and i'll show you here increasing performance by reducing dynamic dispatch so dynamic dispatch and static dispatch is the key here and this paragraph really sums it up nicely so like other languages swift allows a class to override methods and properties declared in the superclasses that's what we just talked about this means that the program has to determine at runtime which method or property is being referred to so that basically says like well if this method can be overridden uh me the at runtime i got to figure out which method we're using are we using the original or we're using the overridden one so that adds some like performance overhead and that's called dynamic dispatch so there's trade-offs right dynamic dispatch increases the language expressivity so it means like you're more flexible with the language right you can override functions however it comes at a cost of like the runtime overhead so if you use final as you see down here and private's another thing with like access control it's another topic i do have videos on that if you want to search the channel but by making a class final or a function final you make it static dispatch which basically means like hey this is final this is the one we're using right with dynamic dispatch the compiler has to like figure out which one we're using by marking something final like that's it you're kind of making that decision for them again performance gains is the moral of the story and again the bigger your app is the more gains you're going to see so that's the final keyword in swift uh so if you like my teaching style you like what i'm doing here you like how i present things i started creating my own courses you can check out the website here you can watch like the first 10 of each course for free just to see if you'll like it or not but anyway we'll see in the next video
Info
Channel: Sean Allen
Views: 5,034
Rating: 5 out of 5
Keywords: Swift, Swift Tutorial, iOS Developer, iOS Development, Swift Code Tutorial, Swift app tutorial, Swift final keyword, final in swift, final keyword swift, swift keywords, Sean Allen, keywords in Swift, swift final class, swift final method, swift final function, Swift basics, Swift for beginners, Beginner Swift, Swift fundamentals
Id: XHrpcUqig3g
Channel Id: undefined
Length: 5min 17sec (317 seconds)
Published: Wed Sep 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.