Kotlin Interface Tutorial with Example - Kotlin Multiple Inheritance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will learn about interface in kotlin an interface is similar to abstract class that we learned about in the last video but there is a difference between interface and abstract class and we will come to the difference later but first understand that interface is same as abstract class and it can contain abstract functions and abstract members and it can also contain function with implementations now you might ask that what is the need of interface it seems exactly like an abstract class so let's understand what is an interface and why we have it so for the sake of example i have some classes and files ready here so i have two normal classes that are video and audio and both these classes are inheriting downloader this downloader is an abstract class and i have one more class here it is also an abstract class but i am not using it for now so i will come to it later so we have two classes audio and video and i have an abstract class that is downloaded inside downloader i have an init block a normal function and an abstract function so very simple and straightforward code now let's understand the difference between an abstract class and an interface so in an interface you cannot have init block or constructor but for an abstract class you can have constructor and internet blocks you can have primary constructor you can have secondary constructor and you can have init blocks in the abstract class but in interface you cannot have constructors or init blocks now another difference is when we extend an abstract class we need to call the constructor because abstract class can have constructors so if you do not have any constructor in your abstract class there is a default constructor and that is why whenever you inherit an abstract class you need to write parentheses like this but in case of interface there are no constructors and that is why you cannot write parentheses when you inherit an interface so if i delete this parenthesis now i will get an error because it says this type has a constructor and thus must be initialized here but in case of interface you cannot write this parenthesis so this is the basic syntactical difference between an interface and an abstract class now let's understand why we need an interface so here i have the downloader where we have the function download and i am overriding that function here inside audio class and video class and both the classes are working absolutely fine i have the main function and here i am creating the instance for same thing that is downloader so i have a reference of downloader but i am creating an instance of audio and i can do it because both audio and video inherits downloader so both audio and video is downloaded and that is why i can make the type as downloader and i can assign either video or audio so after creating the instances i am calling the functions so if i run the code now i will get the output so it is working absolutely fine now the problem with abstract class is what if we want to inherit one more abstract class and that is why i have created this player here so i have downloader and i have player now if i want to inherit player as well to my video class what will happen let's try so i can write here player like this and you can see we are getting an error now the error says only one class may appear in a super type list so the problem here is kotlin do not supports multiple inheritance so whenever we are inheriting a class we can inherit only a single class so here we cannot have multiple classes and to solve this issue we have interfaces so now to solve the problem what i can do is i can make my downloader an interface so to make an interface you just need to use the interface keyword like this and because it is an interface you cannot have an init block inside so i will delete the init block you can have function with implementations no problem and you can have abstract functions but because it is an interface all the functions or members are abstract by default so unless you don't have the body the function is abstract and that is why you do not need to use this abstract keyword so we can remove it from an interface now this interface is same as the last abstract class downloader but in this case we do not have the init block and we do not have the abstract keyword you can put abstract keyword but that is optional and not needed because inside interface all the functions and members are abstract by default the same thing we can do with player so what i will do is i will make it interface like this i will remove the endnet block and i can remove this abstract keyword and i will remove it because it is not needed so i have an interface player and i have an interface downloader now what i can do is i can inherit both downloader and player so because it is an interface now we cannot have this parenthesis because interface do not contain constructors so let's remove the parentheses like this so we have class video i will do the same thing inside this audio so now the error is gone from the audio class but inside video we have some error and it is we need to override the public open function that is downloader info so one more thing that you must care about is when you have two and both your interfaces contains a function with the same name so i have this downloader info function inside downloader and i have the same function inside this player as well so these functions are not abstract but because the name is similar in both interfaces we need to override that downloader info function as well now to solve this issue what i can do is i can change the name to let's say player info and now you can see the error is gone and instead of this download function i will make a function here that is play so now you can see here that i can inherit multiple interfaces in my video class so i have downloaded and clear both the interfaces here so because we have one more function that needs to be implemented that is inside my player interface we need to override the play function as well so we can do it very easily let's override so i will write here playing video name like this so now in this class video what we are doing is we are inheriting downloader and player both the interfaces and we are implementing the function or we are defining the implementation for these download and play functions because these functions are abstract inside our interface so now we will do the same thing with audio so i will copy both the functions and i will paste it here like this and here also i will write downloader and not audio but player like this so i will change the video to audio in both places like this so now we have a class that is inheriting multiple interfaces so when you need to use interfaces for now understand like this whenever you want to use multiple inheritance you can use interface there are more use cases and we will discuss about them in coming videos for now if you want to use multiple inheritance that means you want to inherit multiple things for your class you have the option of interface so here i am inheriting downloader and player and if you want you can inherit a class as well so here you can extend a class and any number of interfaces we don't have a class abs here so we can make it for the example let's say open class abs like this and now we can inherit abs and any number of interfaces so this is the use of interface so i will read this abs for now i don't know why i have written this name abs so that is the basic use of interface now i will show you by running the code so it is basically the same thing this time also i am creating downloader and i am calling the functions that are download downloader info download downloader info so let's run it first so if i run the code i will get the same output now you need to understand that here we are creating an instance of downloader so the type here is downloader and we are creating an instance of audio and that is why you cannot call these functions from this downloader because these functions are not inside downloader so if you try to call the function let's say play you are not getting this function because this function is not inside downloaded and we have the reference of downloader and not the audio we can do it because this audio inherits downloader so what we can do is we can also create an instance of audio directly and video directly and now we can call audio downloader dot play and audio downloader player info and the same thing we can do with video downloader like this now let's run the code and you can see we are getting the output it is correct so that is all for interfaces and kotlin friends in case you have any question or confusion you can leave your comments below thanks for watching you
Info
Channel: Simplified Coding
Views: 3,153
Rating: 4.8709679 out of 5
Keywords: kotlin interface, interface kotlin, kotlin multiple inheritance, kotlin programming tutorial, kotlin android tutorial, android kotlin tutorial, kotlin for beginners, kotlin programming, kotlin oop, object oriented kotlin programming, kotlin inheritance
Id: DuJgXvtYvJ8
Channel Id: undefined
Length: 12min 54sec (774 seconds)
Published: Mon Jan 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.