Important Java Interview Questions - Part 7
I'm sharing my experience with you
as these are the questions which have been asked to me while interviewing on
java profile.
Where have u used abstraction and
encapsulation in your project?
We use encapsulation everywhere in java like when we create a class in
java we are using encapsulation. Abstraction
we use when we create abstract class or interface using abstraction in java.
"Encapsulation is to hide the
variables or something inside a class, preventing unauthorized parties to use.
So the public methods like getter and setter access it and the other classes
call these methods for accessing"
Using Encapsulation we achieve “Data Hiding, Information Hiding” in
java. Encapsulation binds data (attributes and methods) and the code in the
form of a class. Encapsulation is the technique of making the fields in a
class private and providing access to the fields via public methods.
Encapsulation provides “access control” through which we can control the
access of class in accessing methods or members of class.
Abstraction on the other hand, can be explained as
the capability to use the same interface for
different objects. It involves the facility to
define objects that represent abstract "actors" that can perform work,
report on and change their state, and "communicate" with other
objects in the system. Different implementations of the same interface can
exist. Details are hidden by encapsulation.
Explanation with live example:
Encapsulation
Function of button is to start the functioning
of machine but did you ever think that what happens inside machine when button
was pressed like inside mechanism of machine. Actually we do not need to worry
about that internal functionality of washing machine till the time our machine
is working fine that is called encapsulation. You don’t "need to know the
internal working of the washing machine to operate" with it. You have an interface to use the device
behavior without knowing implementation details.
Abstraction
To explain you about abstraction in
java we can take three different entities (People in real world).
You: As a
user you know how to start a washing machine. We just need to press a button and
all other operations behind the scene are happening and are abstracted from
user.
Now you are facing some problem with your machine when you are
pressing start button it is not turning on there might be some major issue with
your hardware which needs to be corrected. Then you call local mechanic to
check the problem.
Local Mechanic: Our local mechanic knows
some of the implementations of starting machine. He can open machine and check some
wires and connections etc which means Local Mechanic knows some of the
implementations of machine.
Now the situation arrived where we have to call washing machine
manufacturing company service center and ask them for service of machine. They will
send an expert to check the problem.
Expert: Since our expert (Designer of the
machine) mechanic knows all the operations of our machine, he we repair it fast
and will give you resolution of your problem. So in short Expert Entity knows
all the implementations of the machine.
The
machine's operation is completely abstracted from you and it is partially implemented
to Local Mechanic and fully implemented to Expert. So you are an abstract
class having only abstract methods, Local Mechanic has extended You(Since
he is also an ordinary user) and he implemented some of the methods and last
our expert extending Local Mechanic and implementing all the methods.
Also
in terms of complexity "Whenever abstraction decreases, complexity
increases"(Since our Expert Entity has very less abstraction, his
complexity of work also increases)
In
Java, garbage collection is also a best example for explaining
abstraction.
If you still have any question you
can reach me any time through email (jigyasu2010@hotmail.com)
Comments
Post a Comment