Abstract Class vs Interface in C# | C# Interview Questions | Csharp Interview Questions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to quespond's youtube channel and what are we learning today today we are going to go and talk about the difference between abstract class versus interfaces when you talk about she shop interviews this question is one of the most asked questions so i'll be discussing about the core difference between abstract class and interfaces also i'll discuss about the specific tipping point you know where abstract class looks very similar to two interfaces and after this lesson i am pretty sure that you will be able to confidently answer this question during the interviews right and very quickly i want to just you know give a heads up here please go ahead and watch my shop interview questions watch my angular interview questions watch my asp.net interview questions and watch in a lot of my interview questions you know which i've hosted on my youtube channel and also i would like to make an announcement out here i have launched my she shop interview questions and answers video course you know which discusses around 60 plus questions you know questions you know which uh like garbage collector you know questions like stack and heap and value type reference type multi-threading delegates events abstract classes object-oriented programming polymorphism and so on so i would suggest that please go ahead and visit my website questpond.com and watch this course so let's get started the difference between abstract class versus interfaces now before i put a header answer i would like to make a statement here with all humility and with all respect to all the interviewers out there this question of comparing an abstract class and interface is like comparing apples and hills yes you heard it right it is like comparing apples and hills so let's first start with the perspective of abstract classes an abstract class is a half defined base class i repeat this sentence an abstract class is a half defined parent class right for example you can see that i have created a class customer here so a customer who comes to the shop you know we'll have a name he will do some enquiry and probably we will give him some discounts now let's assume that your requirement further details saying that there are different types of customers so you have customers who are online customers customers who are platinum customer gold customer discounted customers customer who walk in customer who talk on the phone customer who come through online right and then you start thinking in a generic way and you say that okay whatever type of customers are there they have a name they do inquire about the product but the discount calculation varies right so what you do probably you think in a more generic manner and you make this as an abstract class you say that okay this is an abstract class customer whose name we have defined it enquiry also we have defined the logic for the inquiry but this discount calculation varies right so what you do you say that this method is abstract so you say that the child classes whichever are the child classes will go and implement and define their own discount method right so you can see now the boundary or the use of the of the abstract class is very clear so in other words now i can go and i can say here public class let us say platinum customer right so this is a platinum customer he will inherit from customer so the name the enquire then the enquiry everything is same but the discount calculation will be something different here in the same way it must be you have a walk-in customer so let's say a customer who walks in right he also inherits from the same customer class and he also defines his own discount calculation and so on right so that means that this customer class is half defined and later the full definition is provided by the child classes now when you come to interfaces when you talk about interfaces interfaces just have signature so you can say here i customer and this will just have signature saying that a customer will have a name a customer will have a enquiry a customer will have discount calculation logic so you can see here interfaces just have signature right and all the methods of interfaces are public so you can see just signature nothing else just signature discount this and this so if you look at the big technical difference is that in abstract class i can define some logic and something and some logic is not defined while in cases of interface i shouldn't be defining any kind of logic it is just the structure it is just the signature let's pause for a while and think why are interfaces empty with just shell methods and signature interfaces are kept without logic so that developers can focus on abstraction i repeat this sentence interfaces are kept without logic so the developer can focus purely on abstraction focus on planning focus on what are the most important things that has to be shown for that entity for example for this customer what is the most important thing that has to be shown name enquiry and discount now what happens is when you look at abstract classes or any other class you start writing the logic once you start writing the logic right it is very difficult that you can think about pure abstraction because once you start writing the logic that means that you already started executing you have already started coding so interfaces is like a planning phase you know where we pause for a while we look at those empty methods and we think that what is the most important thing that has to be shown outside once you know we know that yes these are the most important things to be shown outside we say that then every customer which is there in our project right should have these properties and these methods right so interface is like a planning phase where we plan our abstraction where we plan what has to be shown for that class outside later on you know when we start writing the concrete classes we don't focus on abstraction we actually try to follow that abstraction which has been planned and we write the logic so this is the planning phase while writing a class is the execution phase so so during planning you know thinking about code can lead to a bad abstraction once you once you start thinking about code right your your abstraction can be wrong as well we can also conclude that there is a proper sequence of creation between the interface and the abstract class first the interface is created this is where we have the planning phase this is where we do the abstraction this is where we think you know that which are the most important things that has to be shown outside then we say okay across the classes in this whoever implements this i customer what are the common logic so all that common logic we define in the abstract class and whichever logic you know has to be implemented by the concrete class below we will we leave them half ended and then finally we have the concrete classes you know where the full logic is defined so first the interface is created second the abstract classes is created where we define the common logic and finally the full classes is created now it is not necessary that in a project developer follows it but i'm saying that if you have to put all of them together then this is the big picture you know what what should happen interface abstract class and concrete classes and when it comes to interfaces and abstract classes you will always see that developers say that interfaces are implemented while abstract classes are inherited the interviewer would love to hear this that interfaces are implemented they are contract so they are forced so even though here you can see the sign is same like you know the same colon sign for inherit like inheritance but this is imp implementation so interfaces are implemented while abstract classes are inherited so in the interview never ever try to say that interfaces are inherited say that interfaces are implemented while abstract classes are inherited as interfaces enforces things because they enforce a contract between the caller and the callee or you can say between the client and the library creator this provides great advantage in terms of any kind of unwanted impact analysis which can lead to a lot of confusion in the system means for example now because you know interface has this contract right it's it's it's enforced contract now what happens is for example you can see here you know in two places this i customer is used so i customer uses the walk-in customer and here we again have c1i customer using the platinum customer now the library creator you know for example the person who is creating this walk-in customer assume you know or let us say the person who is writing this abstract class he cannot go and he cannot change this method names randomly for example sometimes the library creator right the person who is creating the customer class can go and say okay i want to go and change this name to enquire so rather than having enquiry i want to change it to enquire if i did not have an interface you can see now at this moment i have an interface so it is complaining that the customer does not implement enquiry but if i did not have this interface i have no idea what can be the impact analysis of this for example you can see the time i have changed this method here that is impact analysis in lot of places so by having an interface here by having this strict contract right now i need to go and follow the interface so once i start following the interface everything falls in place so one of the biggest advantage of this enforcement of contract is that between the client and the library creator or you can say between the caller so this is the caller from here we are calling and the caller and you know to whom we are calling right there is a strict contract so nobody can just go and break anything right and if you even if you want to break you know then there has to be a certain agreement before you try to break it so again this is one of the biggest advantage of an interface so very quickly if the interviewer is asking you what is the difference between the abstract class and the interface right the first important thing about the interface it should come out is that interface is where you plan your abstraction interface is a contract interface you know enforces thing to the classes while abstract classes abstract classes are half defined parent class if you want to go and share something in a parent child hierarchy right that's where you know you use an abstract class interfaces are implemented while abstract classes are inherited so both of them have complete distinct behavior or they have you know distinct areas you know where they serve but then why is this question asked in the interview again and again that what is the difference between an abstract class and an interface so let me come to that point now this abstract class which is an half defined parent class if you stretch this abstract class to a level if you go and if you stretch this abstract class to a level you know where you make it completely abstract means what i mean is for example let me go and comment this so let me comment this so let us say now this abstract class out here if i go and if i make all the methods as abstract now look at this if i say this enquiry is abstract if i say this property is abstract right you can see that i have i'm i'm making all the methods and the properties as abstract so now if you see here this is the interface and this is the abstract class now if you ask me technically there is no difference because if i implement i customer i have to implement all the methods you know for example if i implement i customer because this is an interface i have to implement all the methods in the same way if i implement the abstract class customer here also i need to go and implement all the methods right all the methods all the properties so technically there is no difference but what we have what we are trying to do here is that we are trying to say that sword can also cut vegetables absolutely you pulled up this abstract class to such a level you made it so abstract that now it is an interface right so if this is a situation then what is the difference right but again this is a fundamental mistake actually abstract class are supposed to have something half defined but now you know because there is a technical loophole and you exploited that technical loophole and you said that no like i would like to make all of my methods abstract definitely now it is looking like an interface right but in this case also there is a there is an issue right so let me talk about it now assume that you have this walk-in customer right so a person who is a walk-in customer you say okay i'm inheriting definitely this is still inheritance but technically this is implementation now right you are implementing like an interface fine so as a walk-in customer you know he comes he inquires he gets some discounts great right now let us say you have some other customer let's say a platinium customer right he also inherits from this customer and definitely he has to implement everything okay this is also good but now let us say you have a lead and you say okay this lead a lead means what a lead means a person who comes to your shop he has a name he does enquiry but he does not buy anything from you right so he looks like a customer you know completely he does everything like a customer but the only thing what he does not do is that he does not buy anything from your shop right so now let's say that you say okay the lead also looks like a customer and you force him to use this abstract class now if you see this here if you if you watch here yes lead has a name lead has enquiry and lead has a discount now lee does not buy anything so having a discount over here is a problem now you are stuck so you can see now now this abstract class you know which is acting like an interface right is forcing everything down below its child classes so must be now you will say that okay let me let me go and change this let me create two abstract classes no problem so you can see here i have created a separate class called as an enquiry customer who will not have discount because he does not buy anything he will not have discount he will only have name and inquiry while the customer will have name he will have enquiry he will have a discount and so on right so what we will do is this customer will have discount calculation as well right so now you'll say that okay um you know if you say you want to only let's say for example a lead you will say a lead will inherit from enquiry customer fine a lead will inherit from enquiry customer and he has a name he has the enquire method very good now what about the walk-in customer so i'll say okay walk-in customer walk-in customer will inherit from both of them he will inherit from enquiry customer frank fine fine he will inherit from inquiry customer because you need the name he will definitely inquire right and he will say that okay like he will also inherit from customer he will you will also say that he will inherit from customer but now in abstract classes you cannot do multiple inheritance in other words you can't do multiple inheritance at all in dot net in c sharp right so this is not working right so you can't do this you can't say take something from enquiry customer take something from customer and create a walk-in customer now you can see that you are stuck you are stuck here because abstract classes are inherited and there is no multiple inheritance for abstract classes and concrete classes so the sword you know by which you are cutting vegetables it is probably you managed to cut a carrot probably you managed to cut a cucumber but now you can't cut a garlic it is becoming difficult right but now let us look at interfaces right so if you look at interfaces in interfaces we can do multiple inheritance so in interfaces i can do like this one is i customer and i can say one is i enquiry so i enquiry right so enquiry has the name and he will inquire right while uh in case of customer he just has a discount now here i can say yes so let me go and delete all this now because interfaces allows multiple inheritance so you can see here i can say i inquiry comma i customer so he gets a name and an enquiry from here and customer gets a discount from here and this perfectly works well while if you just say enquiry customer right you just say i inquiry here right if you just say i enquiry what happened ah let me delete this you know i had this abstract class here so so now i inquiry let me you know i had created this abstract class let me comment it so i inquiry yeah so this i inquiry now implement interface now this will only have name and enquiry so you can see because you know interfaces can plug and play right because it can do multiple inheritance it is becoming very difficult so coming again to that point so what's the difference between a pure abstract class and an interface well technically yes that abstract class looks like an interface it can work till a certain extent but it is a wrong design it is a wrong design an abstract class should have some implementation if it is not having some implementation then the existence of that abstract class is doubted so just answering this this specific scenario which comes in the inter in the interview that what if i have a pure abstract class yes if you have a pure abstract class it becomes like an interface but then there are these scenarios you know where suddenly you need multiple inheritance and multiple inheritance is not possible in an abstract class and that can lead to technical issues you know as you go ahead with complex scenarios now if you see this example this is a classic example of abstraction failure this is a classic example you know wherein we failed to identify two different abstractions one abstraction was you know we have a customer who has who just comes from inquiry and another one we have a customer who enquires and also buys something right so that's why you know interfaces helps you to identify such close abstraction of families the whole goal of you know where in the interfaces do not have any logic is that it gives you a coolness of mind it says that do not think about the logic first thing that what are the abstractions of your project right so you can see here interfaces helped us to identify abstraction like there is a customer and there's a customer who does not buy who just inquires and a customer who buys now later on once you have identified the identified the family of abstractions then you can have abstract classes so across that family you can say that okay these are the common things and you can apply abstract classes so first start with the interface and identify the family of abstractions once you have identified the family of abstractions then across that family create a common abstract class you know which will have those common logic you know which is shared among the concrete classes so something is half defined and something is later fully defined by the concrete classes so let us try to summarize now what we have learned so that you know we can put down uh we can answer this question properly before the interviewer point number one interface is a contract interface helps us to identify abstractions while abstract classes are half defined parent class and during the interview always emphasize that abstract classes are inherited while interfaces are implemented there are scenarios you know where you can make an abstract class completely an interface there are scenarios you know wherein the abstract class can be pulled up technically and you can make it a pure interface but then that's that's there is some design flaw out there right even if you're able to do it later on you know when it when it comes to multiple inheritance you will have issues so interfaces have multiple inheritance while abstract classes do not have multiple inheritance in abstract class some of the methods are defined some of the logic are defined while some of the methods can be abstract but in case of interface everything is abstract you have to implement all the methods so that brings us to the end of this video i hope that you enjoyed this video please do visit questman.com please watch my shop interview questions and answers video course which has around 60 questions happy learning happy job hunting
Info
Channel: Questpond
Views: 31,871
Rating: 4.9402986 out of 5
Keywords: c#, .NET, interface vs abstract classes, abstract class vs interfaces, difference between abstract classes and interfaces, interface, abstract class, c# interview questions abstract classes, c# interview questions, object oriented programming abstract class vs interfaces, full abstract class vs interfaces, why should i use interface, why do we need abstract class, interface vs abstract class with example, c# abstract class, c# interfaces, c# fresher interview questions
Id: 0EnSPBVrbG0
Channel Id: undefined
Length: 26min 6sec (1566 seconds)
Published: Sat May 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.