9

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

Fazeela Abu Zohra
  • 641
  • 1
  • 12
  • 31

6 Answers6

3

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.

Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

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.

Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89
0

Encapsulation is wrapping of data into single unit. Example class

Abstraction you got right

AJ.
  • 4,526
  • 5
  • 29
  • 41
  • Doesn't answer the question, and access specifiers have nothing to do with abstraction. -1 – user207421 Dec 06 '13 at 06:13
  • @EJP `private` and `protected` are used to hide the implementation from outside the class. Isn't it? – AJ. Dec 06 '13 at 06:19
  • access would be a better term that implementation and that is encapsulation. – Aniket Thakur Dec 06 '13 at 06:26
  • @AJ. Abstraction is the separation of interface from implementation. Nothing to do with access control. Consider Java interfaces for example. The methods are public but the implementation is separate. – user207421 Dec 06 '13 at 06:41
0

Abstraction is the practice of making a base class 'abstract' and then extending its functionality. An abstract class is something that doesn't exist in a concrete matter; its only purpose is to be extended. Think of if you were writing classes to represent different species. All of your different species might extend an abstract Animal class because they would all share common attributes as animals. However, you would never instantiate an Animal object, because every animal you see in the world is a squirrel, or a dog, or a fish ... or some kind of concrete implementation of that base, abstract animal class.

Encapsulation is the practice of making your class variables private, and then allowing access to them from get and set methods. The purpose of this is separate the way your data is accessed and the way it is implemented. For example, if you have some variable that has a requirement, that every time it is changed, it also increments a second variable by 1, then you would encapsulate that functionality; that way your code is more reliable because you don't have to remember to adhere that rule every time you'd access the original variable.

If you want specific code examples, I'd recommend just doing a google search, because there's a lot of examples like that available. Here's two:

http://www.tutorialspoint.com/java/java_abstraction.htm http://www.tutorialspoint.com/java/java_encapsulation.htm

Mr.G
  • 3,413
  • 2
  • 16
  • 20
0

I would say Encapsulation is way of implementing data abstraction.

Encapsulation means wrapping up of data and functions in a single unit.

Abstraction is what we get after doing encapsulation.

batman
  • 267
  • 2
  • 11
0

From this answer

Abstraction - Abstraction is the concept/technique used to identify what should be the external view of an object. Making only the required interface available.

Encapsulation - Is binding of data and related functions into a unit. It facilitates Abstraction and information hiding. Allowing features like member access to be applied on the unit to achieve Abstraction and Information hiding

Community
  • 1
  • 1
Prasad
  • 1,188
  • 3
  • 11
  • 29
  • That's not 'the source'. That's just a rather poor answer to another question on StackOverflow, which has zero votes. Another answer has over fifty, and quotes a real source. Why you're quoting this one is a mystery. -1 – user207421 Dec 06 '13 at 11:00
  • @EJP I thought that this would help OP to get his concept clear. in easy language. – Prasad Dec 06 '13 at 11:40
  • It probably does but it's a strange way of citing it. You didn't even link the answer, just the question. – user207421 Dec 06 '13 at 23:10
  • @EJP I was unaware of linking the answer. thnx for editing. now why downvote at this time? – Prasad Dec 07 '13 at 05:54