Thursday, October 13, 2016

OOP interview questions :- What is the difference between Abstraction and Encapsulation ?


abstraction and encapsulation because i think is reason is if you look at the definition of abstraction and
encapsulation they are very much similar for example when we look at abstraction the definition of the
abstraction says that abstraction is nothing but showing only what is necessary and while encapsulation says
height complexity so it is like we are trying to say same thing but probably we different sentences and i think
that's why many of the developers are confused what exactly abstraction and what exactly encapsulation so i
thought that in this demo i will try to demonstrate actually what is abstraction and what is encapsulation
Now let's assume that you have requirement of building up very simple customer screen so you can see that i
have put up nice prototype there that how customer screen look like so let say that you want basically go and
develop this customer screen and you can see there this customer screen takes in customer code it takes in
customer name and it has very simple add button which will actually go and add this customer to the database
so looking at the requirement you then give thought and say ok what do i need actually i need to create a very
simple customer class which has a customer code it has a customer name and a function called as add which
will help me to add this customer to the database so looking at the requirement then you switch back to your
visual studio you start creating customer class with two properties must be customer code customer name and
ADD button so let me just switch back to my visual studio let's start creating our class so looking at my
requirement what i need first is i definitely need a customer class i need a property called as customer
code because that will help me to take customer code from the user interface i will need one more properties
called as the customer name and i will need a very simple function called as add which will help me to add
to database my database code will go here now if you watch very closely this step what i have done in this step
i have just look into the basic things what i need from the customer class in other words i said ok for this
customer class i need customer code i need customer name and i need add function that's it i have yet not gone
into details of thinking that in the add probably i will need ADO.NET connection or how do i go and validate
my customer code how do i go and validate my customer name etc..
i have just try to answer the what part of the system saying that what does my customer class needs so that
anybody can use this customer class once i have identified what my customer class needs now the next
thing probably which i need to answer is how do i go and implement all this things so i would like probably go
more in-depth and talk about that how will this add function go and add to the database how will the
customer code and customer name get validated so in ordered to answer those questions i need to basically
now start going more granular and i need to defined how will this hole things works how i will the validation work
now how will this add function actually go and add to the database so for that what i do is i am going to talk
more granular i am going to define more granular methods here so one method which i would like to define
here is saying validate now this validate method what it will do is it will actually go and validate the customer
code and customer name i will not write the code at this moment this validate method is responsible to
validate the customer code and customer name validation can be anything at this moment it can be null
check it can be length check whatever it is but this method will be responsible to validate the customer code and customer name
now in the same way we will need one more function which will help us to go and create ADO.NET connection
write Sql statement fire it to database etc. So for the same i will create very simple function here called as
CreateDBObjects now this function what will be do is it will help me to go and create ADO.NET connections it
will help me to go and create Sql statements and fire those on the database
you can think about this is DB connection and SQL etc.. I am not writing the actual code now but you can just
visualise that you know these functions will be meant for the same so I am almost done you know with my class
and this class is now basically it can be used by any one to go and add this customer data to the databse
So what it has it has all the necessary property like customer code customer name it has a very nice
add function it also has lot of granular classes like validate and create DB object which will help us to go
and add this customer class to the database so we are all done now and let me go and recompile this project
just to ensure every thing is fine so we started first with the what of the system and we set that we need only the
customer code customer name and the add function and then be started going more granular
and we define you know what extra method we need so that we can actually go and accomplish adding this
customer class to database so now that we have done with it now this time to go and start using
this customer class using interface so let me go and add a new project a console application project and let me
consume this customer class in to this console application so let me go and add a reference
to the customer class here and now let me start using that customer object OK
so I am go to say CustomerComponent. Customer obj = new CustomerComponent.Customer and i am go to say
obj. so i can see that basically i have the customer code i have the customer name i have the add function
and so i will say OK, CustomerCode="s001" and CustomerName="shiv"
So I first set my customer code and customer name property then i say yes now you go and first validate
these data proper or not then after that go and you know ones validation is successful then go and create
the DBobjects and finally go and add this whole things to the database so basically we have successfully
use this customer class in your project but there is a problem here the problem is these 3 lines of code here
Now these 3 lines of code are actually complicating the whole situation in other words
if somebody ones to use your customer Class he has to understand yes
he has to part the CustomerCode CustomerName then first he has to called the validate function then
he has go and create the database object and then he has to go and call the add function
now that is very complicated processor probably it could be great if can gave him the more simplify interface
so what you do for that is to make this think more simple you go back here and you ask your
some questions you say that OK basically what does my end user when i say end user i am into say the developer
who is actually go into user component what does you want actually and what does you need
in order to use the CustomerClass so he needs is just a CustomerCode he needs CustomerName and he needs Add Function that's it
he is not really he should not be expose to calling the validate function and create DBobject
create connection object those things so we are complicating is life lot so in other words
what you have to expose here in to your console application is just what of the minimum things
what the end developer need in order to use your component and not much more then that
So thinking from that aspect do is that OK you go here and you make this thing as private
you make this thing as private and then you say OK how but calling both of this things inside that function itself
so that we are called and now if you go to your static void main you can happily go and delete both of these
and you have the normal add function only which you need to call now if you see your interface
it is very much simplified now any body can go and use component because you have exposed
only the minimum things what the end developer needs or what the end user needs so lets retreat
what we did step one we first identified what do we need in other words we took 50000 feet level
Kind of a view answer did what does CustomerClass need it just need a CustomerCode and CustomerName
Step two we expanded further and we set that how do we achieve this what in other words
we define to more function called as validate and create DBobject and step three what be did is we set that
we look at interfaces and we set that there exposing to much in other words we are violating the
which we discuss previously so went ahead and be made those complicated methods as private
be fine tune the system now let me answer where was abstraction and where was encapsulation
Now the step one when we were identifying the what part of the system is nothing but abstraction
this is abstraction and
the step three where we actually simplified your interfaces by having the complexity of
validating the objects and creating the database connections or creating database object is nothing but
encapsulation so you can think about abstraction as a thought process or
you can think about abstraction is nothing but show only what is necessary show only what is minimal
ok which is help you to do you work
while encapsulation means basically bringing down complicity
you can also think other way that encapsulation implement subtraction
so you can see basically putting all head together abstraction is third process abstraction is a modeling
term i will say rather
and while encapsulation is actually the implementation
so i hope that this session was useful in this session we are trying to demonstrate practically it is very difficult
to demonstrate but i try my level best to demonstrate what exactly is different between abstraction and
encapsulation
Now whatever video you have seen right it's just glimpse 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 +91-9967590707 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 everything what a DOT NET
developers want's
so write from basics of ASP.NET Object Oriented Programming, Sql Server to new technologies like
WCF, SilverLight, Linq , Azure, Entity Framework we also have UML, Architecture, Estimation, Project management
there is complete invoicing project end to end which is covered so that you can get a better fill of how to
actually create projects in a systematic manner we have covers several product both for Share Point 2007 and as
well as 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 +91-9967590707 and you can ask for the rates it's very decent rate what we have
in the same way as compare to videos we also have one more product with us that is our interview questions book
so we have different kind's of interview Question books wright from Dot NET Interview Questions to Sql Server
Interview Questions
SharePoint interview Questions Biz Talk Interview Questions etc...
so in-case you are interested i the books part you can call on these numbers as per your location
so you can see this numbers on the board at this moment
so i hope that you keep enjoying the videos you keep seeing our site and i hope that you gain more knowledge
THANK YOU VERY MUCH :)