Interface in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning good afternoon good evening from whichever location you are viewing this video from whichever location you are watching this video now before I start this video I would like to thank India mental calm because they regularly organize such kind of fundamental sessions so I would I'm very thankful for them because in today's world you know where people are talking about cloud computing people are talking about data science and data mining and artificial intelligence somewhere you know there should be equal focus on fundamentals on data structures on algorithms on objective programming on SQL right and India mentor is one of those organizations you know who keep doing these kind of programs you know where they they focus on fundamentals so I'm thankful for them because to organize such kind of session you need money you need time you need lot of dedication right second is I would like to also thank respond calm to make this happen because they have organized the mics I've used that studio so I am very thankful for them so in this video we will discuss about why do we need interfaces so here is agenda of this training okay so let me not use that word training because things like interfaces abstract classes object into programming there are lot of these concept you know who have multi perspective so when a developer looks at a class or if they look at our interface or if they look at abstract classes they see different different perspective depending on their experience depending on how they understand it depending how they feel about the topic so rather than saying this is a training video let me say that this video is where I will express my perspective my personal perspective about interfaces and if you are thinking of with that perspective then you can put down the comments below and if you have an old perspective as I've said that when you talk about interfaces every developer has his own perspective right some people say that interfaces is contract and then some say that it is for multiple inheritance and then some say this and that and abstraction and so on so everybody has a different perspective of an interface so rather than saying that this is a training video I will say that in the video I will express my personal experiences about interfaces and then you can comment down in the YouTube that what do you think about these experiences we can discuss em and we can conclude interfaces in a better manner so this is more of a perspective video rather than saying it's a training video but still here is agenda which I have first thing I will discuss about that why understanding interfaces is so confusing why isn't it straightforward like understanding for loops and if conditions second I will talk about the deep relationship between interfaces and contract right third I will talk about my experiences you know when I look at contract so I see contracts from two perspectives one is there is an implicit contract and one is there is a explicit contract so I will also discuss about implicit and explicit explicit contract also I will discuss about the benefits of impact analysis and change management when we implement explicit contracts then I will clarify some statements you know which people discuss about interfaces like interfaces cannot be changed interfaces is for versioning by using interface we can implement abstraction interface is for multiple inheritance so basically I will discuss those topics and clarify them from the point of view of interfaces so this is a 3035 minutes of video so let us start now before we deep dive into interfaces before we try to understand that why we need interfaces let us first try to understand that why is understanding interfaces so confusing why is that developers keep discussing about interfaces and they come out with different conclusions in different times right if you see interfaces from the perspective of a developer every developer looks interfaces from different different perspective because interfaces has multiple uses some developers depending on their experience depending on what they have learnt or depending on what they have understood understood some developers can say that interfaces is for multiple inheritance some developers can say that interface is a contract it helps you to do standardization it helps you to apply abstraction it is like a contract and so on so basically because of the multiple use of interfaces every developer sees interfaces from his own perspective now because of such kind of multi multi use scenario we never conclude what is the real benefit of an interface or what is the real use of an interface so every developer is right in their own perspective and the conclusion never happens when you have such kind of a scenario to resolve this we should try to hunt the root of that entity's existence for example when you talk about a mobile phone a mobile phone can have multiple users you know you can send an SMS you can check an email you can do lot of things right or do social networking and so on but at the end of the day the final use of a mobile is for speaking purpose so even here we should try to hunt that what is that root reason of interfaces existence so let us try to analyze that so with all that definitions you know which are floating around and with all the discussions whatever I had with my friends the experiences you know what I had in a project what I understand is that interface is a contract and the other things like you know multiple inheritance versioning standardization and decoupling enforcement all these are by product of a contract so the basic use of interface is that interface is a contract so let us first try to understand what is the meaning of contract in the real world and then we will see from the perspective of the software code a contract in a real world or in an actual world is nothing but it isn't legal binding between two entities so a customer can have and contract with a supplier wherein the customer says that yes I'm going to pay you X amount of money for this product and the supplier promises to deliver that product in the X amount of days so a contract is a legal binding which two entities decide to follow and if any one of them breaks it then you know that transaction is not possible let us try to understand from the source code perspective from the from the from programming perspective what exactly is a contract so now in application development it is very much normal that one part of the code can call the other part of the code so if you look at developers designing mindset you know they develop components in a modular way so you can have an accounting component you can have an invoicing component and the invoicing component can make a call to the accounting component so normally developers when they do development they create modules and one module can call the other module so you can have one module calling the other module you can have a client calling a server code you can have a UI consuming and component so it is very much normal that to increase usability or to you know do modular development we normally have a situation where one part of the code calls the other part of the code so for example here you can see I have an UI this is some console application I can rename this so here is a very simple situation where I have a user interface this user interface is a console application and this console application is consuming this maths component so you can see that I have a caller and this is the Kali right so we normally have a situation where one part of the code calls the other part of the code now in order that this this UI now this UI which is consuming this maths component has certain expectations so he says that okay from the maths components he has expectations like you know the maths component should have methods like add subtract multiply divide and so on so basically the UI developer who is consuming the maths component so for example when the UI developer goes ahead and consumes the maths library so I'm going to go and this is my UI code so in this UI code I'm going to go and create an object of the maths library now this this caller this client this UI has certain expectations he says that see I need to find the add method and the add method should have two parameters like number num1 and num2 or i need to find the subtract method right so if she does not find this method if for some reason this maths component does not have an abstract method right then this call is not possible so you will see a red sign over here saying that the subtract method has not has not been found so basically now here that is in contract there isn't contract between the UI and the maths component there are some expectations the UI says that I need to find these methods or else this transaction is not possible so when you talk about application development or when you talk about programming there is a contract between a collar and a collie there is a contract between a client and a server there are certain expectation between the UI and the component that I need to find these methods if these methods are not available then I won't be able to run the application so a contract in a programming world means that you have one code calling the other code and they expect certain methods they expect certain parameters they expect certain data types and if those data types are not found you know then you end up with a error or that call is not possible so this is a contract in a programming world so point number one which I want to stress here is that contracts in application development is always there by default so whenever one part of the code makes a call to the other part of the code there is an implicit contract there is an unset contract there are certain expectations in order that the call should be successful so one thing which you have to remove from your mind is that contracts are only possible wire interfaces no no no no contracts are everywhere whenever you make a call from one part of the code to the other part of the code there is the unset contract there are certain expectations of the and the colleague of the client and the server right so first thing is there is something called as an implicit contract right between two between caller and the callee right so for example over here now there is an contract you know or there is an implicit contract where in the UI is expecting thus abstract method and if he does not get a subtract method the application does not compile this this transaction is not possible so point number one there is always a contract irrespective you have an interface or you do not have an interface now in this particular situation this particular scenario the caller is referencing the collie directly so in other words this user interface is referencing the maths component that's why there is a implicit contract but now let us say for some reason for numerous reasons I will say there are situations you know wherein the caller is not aware of the collie he knows what call to make but he is not in direct contact with the collie for example of situation number one the architect of the project decides that I want to decouple this user interface from the maths component right so that when the maths component changes the user interface does not have to compile so this is situation number one wherein the architect decides that the UI should be decoupled from the component the other situation can be where the caller and callee is not directly connected is because of your physical deployment so you can have the caller deployed in one machine and the collie is deployed on a server right and they do not have any direct contact with each other the only call must be they are making is through some protocol like HTTP or TCP so there are a lot of situations you know where suddenly the caller and callee are not referencing each other directly right so for example now let us take a simple situation wherein the architect decides that this you I should not reference the maths component directly so that you know we have a decoupled architecture so what he does is he probably goes ahead and puts a layer like Factory or some kind of an intermediate layer right so let us say now as an architect I decide that the UI should not reference the maths component directly so I go ahead here and create an Factory in case you're new to factory pattern I would suggest to go and look at the factory pattern videos but for now you can just assume that this Factory layer will help us to decouple the user interface from the maths component right so I'll say this is factory layer so this factory layers references the maths component right so this is factory mats we can make this as static so using maths component right so now this factory layer says that okay I will go and expose out the maths component right so he says here is it maths component or I'll say create maths component right and he returns new maths library okay okay everything has to be static fine in static classes everything has to be static now you can see that I'm using a dynamic keyword here I'll come to this dynamic keyword later on this dynamic keyword is a dotnet keyword you know which says that on the runtime I will decide what is the component one of the methods so in case you are new to dynamic keyword I would suggest to go and see this video in croissant VD where I've explained what exactly is a dynamic keyword right so for now you can leave this dynamic keyword as it is but what here I'm trying to do is I have a factory maths and this factory math says that you know if you want to reference the maths component piece therefore why I mean so that when the maths component changes the user interface does not have to change now look at this situation in this situation the caller and the callee so now here this you I will refer the the factory layer right you will refer the factory layer so factory here right and I will say here now rather than I creating the object I will say that the factory layer the factory maths layer will create the maths object for me right and then I will make a call to substract methods I will make a call to add method and so on now if you see the situation out here in this situation the caller that mean the UI is not a referencing can see here he is not referencing the maths component but he is referencing why are some layers so now in application development we can have we can have numerous such kind of scenarios you know where in the collar is not referencing the colleague code directly right so now if you see at this moment this code will run right so this dynamic keyword what this dynamic keyword does is this dynamic keyword says that I will decide on runtime on the runtime I will decide is the subtract method existing and is the add method existing so if I make a call here so let me just make a call here so I'm going to go and run this code so now the factory mats will create the object of the maths component so there it is he is creating the object of the maths component and then the subtract method is decided on runtime so on runtime it decided yes this is the subtract method and you can see that this is the add method right so this is this is other situation wherein the caller and callee are not connected directly and still we expect that the call should happen right now now over here for some reason for some reason let us say the maths component developer you know decides that this add method should be add numbers so the person who is who has created the component right the collie the collie code developer decides I am going to go and change the code of it and if I compile this project I will not get any kind of errors you know because the caller and the callee have not connected directly right so when I when I rebuild this solution here you know you can see that I am getting rebuild all succeeded so there are no errors right but now what will happen is during the runtime my program will crash because so if you see here now during the runtime when I try to run this I'm going to go and just run this I will get an error saying that this add method does not exist now now we are in a situation where in the maths component has changed and the clients were consuming the Maps component are not made aware that the change has happened in the maths component and this can be catastrophic why because assume that this maths component is consumed by a lot of clients it is consumed in a lot of places right so all the calls of those clients will now start crashing isn't it so now we need some kind of an alert system so during the development time itself I need an alert system wherein someone will say that yes if I go and change something out here it's going to affect here here here and everywhere right so I need some kind of an alert system that's where we need explicit contract now this is a situation where in the caller and the callee are not connected directly and we need an explicit contract that means we need an interface so now over here let us go ahead and add a interface so let me add a new class library and so I'll say this is now IMATS right and in this IMATS so this is the IMATS IMATS interface right so this interface will have a method called as ad and will have num1 and num2 so normally interfaces only have definitions right it does not have any kind of code as such it is just a signature it is just a structure okay so here it is this is a interface so I'll use the interface keywords so this is int add and this is int subtract right so now this is a contract an explicit contract we have written down the interface and we say to both to the caller and the callee saying that both of you guys have to reference this interface if any one of you change it if any one of you do not follow it then this call is not possible right so if you see here now so first thing is let us go to the maths component first the maths component goes and adheres to the interface so I'm going to go and reference the maths component defens the interface and over here I will say using [Music] IMATS all right so now this maths library we will go and implement the maths interface right so you can see now this maths interface has add and subtract and both of them are implemented in case if let us say the component creator goes and gives some different name out here then this interface will start complaining and we say that see you have not implemented the add method so you can see that he made it add numbers and the interface signals him that if you do not make this as an ADD method then the other party who is calling you will have problems right so if I now go and change it to exactly add it does not complain right in the same way even the client will now use this interface to make a call so here also the client will now use the IMATS you can still see that I am NOT referencing the component directly my user interface is not referenced in the component directly it is just referencing an interface right so now here also so here now rather than saying this is dynamic I can say now this is IMATS right so even in the factory rather than making it too dynamic I can say this is IMATS so even in the factory layer I will refer the interfaces so I'm going to enter four interfaces because dynamic is too broad right so dynamic should be used in situation where it is really really run time so I'm at IMATS right now if you see the situation so here I have that is IMATS IMATS interface I'm sorry IMATS is the namespace and this is the interface I'm at synthesis now you can see the interface has created an explicit contract he says to the UI layer that you will get an ADD and a subtract from this and he says to the maths component developer that you need to follow this if you do not follow this then there will be a problem so now this call will be successful again so if you see now they should really work so if I run this so there it is it goes and it calls the subtract method right so now if the maths component developer for some reason goes and says this is subtract something like this he gets an alert here he gets him alert and he says that see if you change this this is going to impact in lot of places are you sure about it right so in other words now for some reason if somebody goes and fiddles around with this if he says oh this has to be now subtract like this then you will suddenly get an alert throughout your system saying that okay it crashes here it crashes now here and it crashes there and it crashes there so if you look at the first situation you know where there was no explicit contract there was no alert system so I you know I did not have any kind of impact analysis of what will happen if I change that ad method or the substract method but now in this case you know wherein I have an interface at place I have an alert system I have an of a way of impact analysis you know where it says that see if you change here it is going to affect in these these places are you sure about it right so interfaces are contract interfaces helps you to implement impact analysis you know in case if you change a method in which which places it will affect so what I feel personally is that interfaces should be used in places you know where in the caller and callee are not connected to each other directly in rest of the situation I feel that interfaces are extra wealth right so for example if you look at the first situation where the caller was connected direct with the colleague and we were okay with it right we were okay with that kind of architecture I own put an interface but in this situation you know where in the column is not aware of the colleague and if anything happens bad then during the runtime I will get errors during the runtime my project will crash so in this kind of situation interfaces need it an alert system is needed and impact analysis is needed and one more very important rule which you will see from every developer about interface is that once the interface is decided it should not be changed so once you decide that the method name is add the method name is sub right you should not change it because if you change it it is going to affect a lot of places but now here it is remember there is one big confusion about when the interfaces should not be changed okay so there is no golden rule as such but when you are doing development when your project is just getting developed it is okay to change the interface means for example I am developing the project it is still it is still not in production so I go and change this ad to add two numbers I can change this subtract to subtract two numbers because I'm developing the product still right but once it goes in deployment once it goes in deployment after that the interfaces should not be changed because once it is in deployment once it is in production then you need to be careful that if you change these interfaces in which which places the impact analysis can happen so let me correct that statement because that statement is too vague interfaces should not be changed once your application code goes in production I again repeat interfaces should not be changed once your application code goes in production the other place you know where I find interfaces to be extremely useful is to create abstractions to create customized abstractions what is abstraction in object of programming abstraction means show only what is necessary so as per the level of the client as per the maturity of the consumer of the caller you only show him methods you know which are important from important for him right for example now let us say that this maths component what you have created has basic maths and advanced maths so some developers want to consume the basic maths and some developer want to consume the advanced maths now the developers who are consuming your basic maths component does not want to get into problems of advanced maths so what you can do over here is you can go and you can create multiple interfaces this is where developers say that interfaces have multiple inheritance right so they say it unknowingly but the reason of multiple inheritance is to create different different abstractions customized abstractions so now what I will do is I will say okay so I have to kind of consumers one consumer who is just interested in the basic maths and there's other consumer who is interested in advanced maths right so I can go and I can create an interface here saying I advanced maths and I will say that this I advanced maths will have everything about IMATS so I can say that this I advanced maths will have everything about I'm I I maths that is this basic maths but he will have something extra as well here so he will have probably a function which will help us to do log to to do to do logarithmic calculations right or he will have methods here you know which says that okay how do I go and calculate the radius of a circle right and so on so basically this I advanced maths has some more mathematical functions you know adding to the basic functions right so now what I can do over here is I can go here and I can say so one is that I can go and inherit from this right so let's let's inherit from this okay so now I can go here and say to my IMATS interface that please go and inherit this I maths I advanced maths right and also implement the rest of the methods so let's go ahead and implement the rest of the methods I had once maths yeah that's so bad implement interfaces yeah that's what I want so you can see that I haven't implemented log and radius now what will happen is so when I look to my maths so also from the factory now you have to return I maths advance so I'm at advanced high maths oh why don't I see i advanced maths yeah so now what will happen over here is now look at it so when a client sees via IMATS interface he will only get add and subtract show only what is necessary but if he creates the I advanced maths so if he goes and creates I advanced maths reference right from this he will see everything he will see the ad also he will see the subtract also he will see the log and the radius also so basically again the other important use of interfaces irrespective the caller and the callee are not connected directly or they are connected directly as I have seen developers using interfaces to create beautiful abstractions so they create multiple interfaces and then they say that okay so if you see Y at this interface you will see only these these methods and if you see why are these interface you will see these these methods so again one of the other benefit of using interfaces or the compelling reason of using interface is that it helps you to define a very basic structure without any code and then it helps you to implement abstractions of different complexity also I would like to speak 10 minutes on multiple inheritance and interfaces because if you see one of the things you know developers say is that the biggest use of interface is multiple inheritance I don't know from where that statement comes in but I have seen that developers the time you say what is the use of interface I have seen 20 to 30 percent developers saying that interface is for multiple inheritance but my personal feeling here is that multiple inheritance of interfaces is a phenomena which can happen once you go in production so when I start designing a project I do not think that I will have multiple inheritance at the start okay means what for example now let's say that this application goes in production so you have an IMAX interface the basic one you have an advanced maths it is happily running you respect the laws of the interface you never go and change these interfaces once it goes in production you never go and remove these methods right because they are all over the place and if you want to remove them then you need to be aware of the impact analysis so let us say that this application goes in production everything is good right now someone comes and says that I want to have a multiply method so you can see that we have ADD we have subtract but we do not have multiply and divide let's assume now I do not want to go and change this interface because it will affect lot of places I do not want to go and change this one because that is also used in lot of places so what I do is that I end up creating a new interface so I say that whatever is my existing code let us not touch it let us not touch it let us create a new interface here so I will say that this is a new interface that's a public interface IMATS IMATS multiply and divide or I'll say I maths maths to write this IMATS - will have multiply and divide you can see that the methods I have just kept it as void for simplicity definitely you know divide and multiply should have some parameters but I just kept it as it is and then what I will do is I will go now to the maths class and I will say here that this one will implement I advanced maths and it will also implement IMATS - you can see now there are two interfaces multiple inheritance what you say right I have implemented so this also we will say implement this interface so in other words you know multiple inheritance you will only do when you say that you do not want to touch the existing interface and you want to go and add new methods so in that case rather than going and changing the existing contract because if you change the existing contract it is going to go and break in lot of places right so you can see over here now we are using multiple inheritance so multiple inheritance is a by-product of contract abstraction is a by-product of a contract right the main use of interface is that it is a contract and all the other things like multiple inheritance and abstraction and whatever we have discussed right is a by-product so let us summarize you know what we have learnt about interfaces in this video so let's summarize what we are learnt first thing we will use an interface or I will use an interface to define explicit contract contract where caller and callee are decoupled are decoupled or they do not have direct references right and the benefit of this is we have a great impact analysis in the build time we have change detection in the build time and because of the impact analysis and change detection in the build time in the run time we will have less problems right so this is the one of the biggest use of interface or I will say at least from my perspective this is the only use of interface the other uses like abstraction which we discussed show only what is necessary is a by-product so the abstraction which we discussed show only what is necessary is more often by-product third one you know multiple inheritance of interfaces will happen only when you go live in production you know where in you have gone life and then you do not want to change the interface because if you change that interface it is going to affect a lot of places so you end up creating a new interface and you end up doing multiple inheritance so here is my take personally on interfaces one of the things you know which I have seen in the IT industry is that people say that for every class you create you should create a interface I do not agree to that because I feel that that's an extra work which we add too so if you have a class that class is never going to change once you create it once you go to production you do not expect too many changes in that why would you like to create the interface in between if you have a class you know which is running direct you are referencing it directly right and you are not creating a decoupled architecture why do you need a interface in between you right so my feeling is that that time I go into a client-server environment the time I go into a kind of environment you know where in my my client and my server are not connected with each other and I just want to ensure that I do great impact analysis right I will use an interface so this video is not training video as I've said you know when I started this video this video is to just create an platform for discussion so if you have any comments on this video I would suggest to go to the below youtube link youtube comments box and put down your comments what do you think about interfaces thank you very much [Music] [Music] [Music]
Info
Channel: .NET Interview Preparation videos
Views: 125,608
Rating: undefined out of 5
Keywords: interfaces in c#, csharp interview questions, interfaces in c# interview questions, what is c# interface, use interface in c#, explain c# interface, real use of interface, explain use of c# interfaces, c# interface example, interface meaning, interview questions on csharp, about interfaces, how to use interfaces in c# project, use interface in c sharp, csharp in visual studio, csharp interview questions for freshers, c# interview questions answers
Id: n4mCbaqH8zs
Channel Id: undefined
Length: 38min 55sec (2335 seconds)
Published: Mon May 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.