Main

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

abstract class vs interface in C# For Full 60 plus C# Interview Questions https://www.questpond.com/c-step-by-step-with-interview-questions---answers-series/cid62 Below is the Full syllabus of C# interview question courses C# Interview Questions and answers Question 1 :- Explain difference between .NET and C# ? Question 2 :- .NET Framework vs .NET Core vs .NET 5.0 Question 3 :- What is IL ( Intermediate Language) Code ? Question 4 :- What is the use of JIT ( Just in time compiler) ? Question 5 :- Is it possible to view IL code ? Question 6 :- What is the benefit of compiling in to IL code ? Question 7 :- Does .NET support multiple programming languages ? Question 8 :- What is CLR ( Common Language Runtime) ? Question 9 :- What is managed and unmanaged code ? Question 10 :- Explain the importance of Garbage collector ? Question 11 :- Can garbage collector claim unmanaged objects ? Question 12 :- What is the importance of CTS ? Question 13 :- Explain CLS ? Question 14 :- Difference between Stack vs Heap ? Question 15 :- What are Value types & Reference types? Question 16 :- Explain boxing and unboxing ? Question 17 :- What is consequence of boxing and unboxing ? Question 18 :- Explain casting,implicit casting &explicit casting ? Question 19 :- What can happen during explicit casting ? Question 20 :- Differentiate between Array and ArrayList ? Question 21 :- Whose performance is better array or arraylist ? Question 22 :- What are generic collections ? Question 23 :- What are threads (Multithreading)? Question 24 :- How are threads different from TPL ? Question 25 :- How do we handle exceptions in C#(try/catch)? Question 26 :- What is the need of finally? Question 27 :- Why do we need the out keyword ? Question 28 :- What is the need of Delegates ? Question 29 :- What are events ? Question 30 :- difference between Abstract class and interface ? Question 31 :- What is a delegate and How to create a delegate ? Question 32 :- Where have you used delegates ? Question 33 :- What is a Multicast delegates ? Question 34 :- What is a Event ? Question 35 :- How to create a event ? Question 36 :- Delegate vs Events. Question 37 :- What is a class and object ? Question 38 :- Abstraction vs Encapsulation? Question 39 :- Explain Inheritance ? Question 40 :- Explain virtual keyword ? Question 41 :- What is overriding ? Question 42 :- Explain overloading ? Question 43 :- Overloading vs Overriding ? Question 44 :- What is polymorphism ? Question 45 :- Can polymorphism work with out inheritance ? Question 46 :- Explain static vs dynamic polymorphism ? Question 47 :- Explain operator overloading ? Question 48 :- Why do we need Abstract classes ? Question 49 :- Are Abstract methods virtual ? Question 50 :- Can we create a instance of Abstract classes ? Question 51 :- Is it compulsory to implement Abstract methods ? Question 52 :- Why simple base class replace Abstract class ? Question 53 :- Explain interfaces and why do we need it ? Question 54 :- Can we write logic in interface ? Question 55 :- Can we define methods as private in interface ? Question 56 :- Want to change interface whats the best practice ? Question 57 :- Explain Multiple inheritance in Interface ? Question 58 :- Explain Interface Segregation principle ? Question 59 :- Can we create instance of interface ? Question 60 :- Can we do Multiple inheritance with Abstract classes ? See our other Step by Step video series below :- For more such videos subscribe https://www.youtube.com/questpondvideos JavaScript Interview Questions: https://youtu.be/Zb4dPi7CANU Learn Angular tutorial step by step https://tinyurl.com/ycd9j895 ASP.NET MVC Core Interview Questions with answers:- https://youtu.be/pXmMdmJUC0g C# interview questions and answers:- https://youtu.be/BKynEBPqiIM C# tutorial for beginners(4 hrs):- https://youtu.be/AxEGRBFwlmI Learn Azure Step by Step:- https://youtu.be/wdUK7bCMXqs Azure AZ-900 fundamentals certification :- https://youtu.be/hC9iGgJorz8 AZ- 204 certification Azure:- https://youtu.be/qI8PRn2C080 Learn MVC 5 step by step in 16 hours:- https://youtu.be/Lp7nSImO5vk Learn Design Pattern Step by Step https://goo.gl/eJdn0m Learn MSBI Step by Step in 32 hours:- https://goo.gl/TTpFZN Learn SQL Server Step by Step http://tinyurl.com/ja4zmwu Python Tutorial for Beginners:- https://youtu.be/KjJ7WzEL-es Learn Data Science in 1 hour :- https://tinyurl.com/y5o7qbau Learn Power BI Step by Step:- https://tinyurl.com/y6thhkxw PHP Interview Questions : https://youtu.be/1bpNSynUrl8 For more details contact questpond@questpond.com OR call +919967590707-9619842789. #AbstractClassVSInterfacesinC# #CSharpInterviewQuestions #C#InterviewQuestions&Answers #CSharpQuestions #CSharpAbstractClass #C#Interface #C#InterviewQuestionsforFreshers #InterviewQuestionsforC#

Questpond

2 years ago

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 tha
t 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 k
now 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 ma
ke 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 chil
d 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 b
een 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 abstractio
n 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 s
econd 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 inheri
ted 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 be
cause 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 he
re 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 inter
face 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 whe
re 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 h
ere 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 a
ll 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 n
ow 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 n
ot 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 be
low 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 calculat
ion 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 th
e 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 a
re 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 on
e 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 sa
y 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 differ
ence 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 abstrac
t 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 w
ho 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 cus
tomer 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 empha
size 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 inherit
ance 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 hun
ting

Comments