I have attended an interview for java programmer ,After a few personal questions ,the interviewer have asked me the question "What is encapsalution and abstraction" I was really happy with question ,and i have answered it as hiding the implementation details that are not necessary for the user to know. And Abstraction is showing of only the important details to user. only seconds after my answering ,the interviewer had another question for me. showing of only the important details to user ,that means hiding irrelevant data like implementation details to user. is it?
I have replied it as yes!!!
There comes another question from .So what is the difference between abstraction and encapsolution .I think there is no difference according to your answer.
I was like i dont know ,My hands were frozen ,was a really bad day for me
Can any one explain how would you answer if someone asks such a question to you
Encapsulation in layman's terms simply means covering(encapsulating).
With respect to Java it means writing your code together and exposing only those part of it which are intended to be exposed.But it is often associated with data hiding. For example when you define your instance method/function private it can be accessed only from the same class. This way you don't expose your instance variables/functions directly. Why? because user has nothing to do with it.
Abstraction again in layman's terms is a concept that is built up to hide the complexity behind it. Take for example computers. They have been an abstraction to processor which do the actual computation which inturn involved chips which inturn involves gates. For common man it would be difficult to talk in terms of gates used. SO the concept has been abstracted out to computers.
With respect to Java abstraction means hiding implementation details from the user. That includes making a class abstract or defining an interface. All user is exposed to is the interface(a set of functions or APIs). He need not know it's internal implementation. All he has to know what input he must provide(arguments) and what would be corresponding output(return type).
For better understanding with Java example refer to this question.
What is Encapsulation?
Encapsulation is nothing but protecting anything which is prone to change. rational behind encapsulation is that if any functionality which is well encapsulated in code i.e maintained in just one place and not scattered around code is easy to change. this can be better explained with a simple example of encapsulation in Java. we all know that constructor is used to create object in Java and constructor can accept argument. Suppose we have a class Loan has a constructor and than in various classes you have created instance of loan by using this constructor. now requirements change and you need to include age of borrower as well while taking loan. Since this code is not well encapsulated i.e. not confined in one place you need to change everywhere you are calling this constructor i.e. for one change you need to modify several file instead of just one file which is more error prone and tedious, though it can be done with refactoring feature of advanced IDE wouldn't it be better if you only need to make change at one place ? Yes that is possible if we encapsulate Loan creation logic in one method say createLoan() and client code call this method and this method internally crate Loan object. in this case you only need to modify this method instead of all client code.
Example
class Loan{
private int duration; //private variables examples of encapsulation
private String loan;
private String borrower;
private String salary;
//public constructor can break encapsulation instead use factory method
private Loan(int duration, String loan, String borrower, String salary){
this.duration = duration;
this.loan = loan;
this.borrower = borrower;
this.salary = salary;
}
//no argument consustructor omitted here
// create loan can encapsulate loan creation logic
public Loan createLoan(String loanType){
//processing based on loan type and than returning loan object
return loan;
}
}
In this same example of Encapsulation in Java you see all member variables are made private so they are well encapsulated you can only change or access this variable directly inside this class. if you want to allow outside world to access these variables is better creating a getter and setter e.g. getLoan() that allows you to do any kind of validation, security check before return loan so it gives you complete control of whatever you want to do and single channel of access for client which is controlled and managed.
What is Abstraction?
An abstract class is something which is incomplete and you can not create instance of abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended. By the way Java has concept of abstract classes, abstract method but a variable can not be abstract in Java. Popular example of abstract class in Java is ActionListener which has abstract method called actionPerformed(ActionEvent ae). This method is called when an ActionEvent is fired like when you click on JButton. Its common in java to attach ActionListener with JButton by implementing abstract method actionPerformed(ActionEvent ae) using Anonymous class, as shown in below Example :
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//code to handle event
}
});
In Java Interface is an another way of providing abstraction, Interfaces are by default abstract and only contains public static, final constant or abstract methods. Its very common interview question is that where should we use abstract class and where should we use Java Interfaces in my view this is important to understand to design better Java application, you can go for java interface if you only know the name of methods your class should have e.g. for Server it should have start() and stop() method but we don't know how exactly these start and stop method will work. if you know some of the behavior while designing class and that would remain common across all subclasses add that into abstract class. Interface like Runnable are good example of abstraction in Java which is used to abstract task executed by multiple thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With