.NET and c# interview questions :- Can we implement interfaces with same method names in c# ?

.NET and c# interview questions :- Can we implement interfaces with same method names in c# ?


Now this is the very interesting question can i class implement interfaces with same method names now this question or this debate is very interesting i know that this question has discussed on lot of fore room people have blocked about it people discuss now and then about this question this is one of Favourite question also during the interview OK so lets try to see practically what exactly happens if we create if we try to create a class which implement interfaces with same method name now first let me try to make you under stand what exactly this question means OK now let say that you have a interface called as I1. which has a method called as Method1 you than have one more interface called as I2 which has again a method called as Method1 now this code is pseudo code so that if you type on your visual studio it should compile but this is just to pseudo code to make your understand to scenario so we have 2 interfaces here I1 and I2 now let say we create a class called as A which implement both I1 as well I2 now both these method names in these interfaces are same that is That is Method1 and Method1 so now what will happen here so what will do is in order to understand this will first go to your visual studio will try to create 2 interfaces will try to implement them in the class and see what happens and then will try to answer this question in a better manner so here the simple console application which i have created so let me go first and create a simple interface here I1 which will have a method for void Method1 Ok and in the same way i will go and create one more interface here called as I2 which will have a method again called as Method1 now let me go and create a class here called as class A which implements both I1 and I2 and let me write some implementation here Public void Method1 i am just go to and put here console.WriteLine saying who has called me OK so we have created a interface here 2 interfaces here with the same method name Method1 and Method1 and we are trying to implement both this interfaces and we have created we have gone ahead and we have implemented Method1 now the first question will be come to your mind is that will this code compile or not so let me go and hit the build here its compiling so now if this compiles so what happens if i create a object of A and point towards I1 and what if i create a object of A and point towards I2 what will happen lets try to see that so let me say here I1 obj=new A and i am go to say invok Method1 in the same way i am go to and create another object of A and point that object towards i2 interface new A I2 obj.Method now what i will also do is i will just go and put a read line here so that we can see the output so i have done now is i am creating the object of A and pointing its towards the interface1 reference and then i am creating the object again of A1 pointing towards the interface 2 reference and i am invoking this method1 so again let see if this code build, yes it builds and now let see if this code runs yes it runs so this works it compiles it runs but this is not what you are expecting right. we have 2 interfaces so we would like to see 2 implementation now to define 2 different implementation for the same method name we need to use excel sheet interface method implementation in other words we need to qualify the method names in the class who is implementing the interfaces by using the interface names so lets try to see how we can do that so lets close this and lets go back to your code here so what is we do here we need to qualify this method name by using the interface name so we have go to say here something like this I1.Method name OK so will say this is called via I1 and the same way when we implement I2 we have to say I2.Method1 so over here i will say this is called via I2 so called via I2 now lets go and build the solution so build solution public is not valid for this item we need to define with us public it is public interface all methods of interfaces are public by default succeeded nice so i am go to CtrlF5 here now what should happen is when i am go to into what you call point this add towards I1 it will actually go invoke this method1 and when i am go to go and point the I2 towards A object it will actually go and invoke this method1 logic so if i do a Ctrl F5 now you can see that the I1 interface is calling is own implementation and the I2 interface is is calling is own implementation so hence for when ever you have a debate stating that can be implement interfaces with same method names yes you can by using the explicit interface implementation so i hope that you enjoy this video keep watching the object into programming section we have go to update more videos in this section and hope that you become technically stronger in terms of fundamentals as well as in terms of understanding dotnet in a better manner Thank you very much ….. Now what ever video you have seen is just gleams of what we have done so in case you are interested in our video package you can go to our site that is www.questpond.com you can call on this number +919967590707 and you can ask the complete DVD package what we have so in this DVD package what we have done is basically we have covered almost every thing what a dotnet developer wants so right from basics of ASP.NET, objective programming, SQL Server to new technology like WCF, Silver light, LINQ, Azure, Entity framework we also have UML, Architecture, Estimation, Project management, that is a complete invoicing project end to end which is covered so that you can get better feel of how to create actually projects in a systematic manner we have cover server products both for Sharepoint 2007 as well as for 2010 we have lot of best practices video on SQL Server etc. so this complete package you can get from www.questpond.com if you are interested and you can call on this number +919967590707 and you can ask for the rate its a very decent rate what we have in the same way as compare to the video we also have one more product with us that is our interview question books so we have different kind of interview question books right from dotnet interview question to SQL Server interview question share point interview questions, wiztalk interview questions etc, so in case you are interested in the books part you can call on these numbers as per your location so you can see this number on the board at this moment so i hope that you keep enjoying the videos you keep seen out site and i hope that you gain more knowledge Thank you very much…..

18 thoughts on “.NET and c# interview questions :- Can we implement interfaces with same method names in c# ?

Leave a Reply

Leave a Reply

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