Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree3Likes

Thread: Abstraction and Encapsulation

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Abstraction and Encapsulation

    Interviewer: What is abstraction?
    Me: Abstraction is the concept by which we are able to use a variable or method of class without actually knowing the inner details of the class.
    Interviewer: What is encapsulation?
    Me: It is the concept by which we make variables and methods accessible outside the class only by creating an object( except in static case where the class name can be used to access a member of the class).
    Interviewer: Is it not the same you told about abstraction? You told very similiar thing for abstraction.
    Me: Well they are similiar concepts
    Interviewer: That's not correct. Lets move onto the next question.

    What is wrong? Please help me

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,733
    Rep Power
    5

    Default Re: Abstraction and Encapsulation

    Two things:

    - your explanation of encapsulation is a bit vague, although I don't believe you were thinking the wrong thing; you just don't have it clear enough yet to properly explain it
    - you confirmed that encapsulation and abstraction are similar concepts, which they absolutely are not

    So what you have to do now is go back a step and restudy both concepts to figure out where you have gaps in your understanding.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    you confirmed that encapsulation and abstraction are similar concepts, which they absolutely are not
    What??

    From the complete reference :

    An essential element of object-oriented programming is abstraction. Humans manage complexity through
    abstraction. For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behavior. This abstraction allows people to use a car to drive to the grocery store without being overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission, and braking systems work. Instead, they are free to utilize the object as a whole.


    Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe
    from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that
    prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access
    to the code and data inside the wrapper is tightly controlled through a well-defined interface.


    How come you are saying they are not similiar concepts? I see both as very similiar concepts with the difference being just the way we look at it.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abstraction and Encapsulation

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    What??
    How come you are saying they are not similiar concepts? I see both as very similiar concepts with the difference being just the way we look at it.
    Ok, you are right and the entire world is wrong.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    Where is the question of entire world is wrong? I was just arguing with him according to the quote from the book. Such a response was not needed.

  6. #6
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    If somebody has an example to illustrate the difference between abstraction and encapsulation, I request you to please post it.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abstraction and Encapsulation

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    If somebody has an example to illustrate the difference between abstraction and encapsulation, I request you to please post it.
    You gave a perfect example yourself when you quoted the reference (reply #3) It's only you who claims that those two concepts explained in those two paragraphs are similar; a fool can drive a car (paragraph #1) but a fool can also ruin that entire car easily (paragraph #2) so the abstraction is fine with a car but its encapsulation sucks.

    Jos
    Last edited by JosAH; 08-25-2013 at 11:45 AM. Reason: wrong reply number ...
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
    Abstraction visit abs
    Last edited by allaudin; 08-25-2013 at 03:53 PM. Reason: JosAH

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abstraction and Encapsulation

    @Allaudin: the OP means a different kind of 'abstraction' here, not Java's implementation detail of an abstract class; read the thread first before you attempt to reply.

    Jos
    allaudin likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    yup ... you can think abstraction as "Tabel of content of Book" that gives short overview of entire book ...

  11. #11
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    edited my thread .. there is no irrelevant stuff now ...

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abstraction and Encapsulation

    Quote Originally Posted by allaudin View Post
    edited my thread .. there is no irrelevant stuff now ...
    It is still wrong.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    k .. Now it will be alright ...

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Abstraction and Encapsulation

    There are several definitions of abstract and abstraction. And I still struggle at times to use the term properly. In the context of this discussion, I tend to think of abstraction as viewing a working system or device as a black box and not caring about the implementation details (nor even aware of them). I think of encapsulation as the act of hiding the details of how the black box functions. So encapsulation is the means to enforce a consistent abstract view of some system.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abstraction and Encapsulation

    Quote Originally Posted by allaudin View Post
    edited my thread .. there is no irrelevant stuff now ...
    Don't keep on editing out your previous (incorrect) contents; it disturbs the flow of the discussion in this thread; others might hit this thread through a google search, only to find the corrupted contents. Next time you do this I'll put back the previous content.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    I tend to think of abstraction as viewing a working system or device as a black box and not caring about the implementation details (nor even aware of them). I think of encapsulation as the act of hiding the details of how the black box functions.
    From your statement it looks like they are related concepts but some people here are claiming they are totally unrelated concepts.

    From the book "Programming with Java - A Primer - E Balagurusamy"
    Abstraction refers to the act of representing essential features without including background details or explainations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and methods that operates on these attributes.
    The wrapping of data and methods into a single unit(called class) is known as encapsulation
    Coming back to the question, first, are they related concepts or not?

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abstraction and Encapsulation

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    From your statement it looks like they are related concepts but some people here are claiming they are totally unrelated concepts.
    Coming back to the question, first, are they related concepts or not?
    Have you read my reply #7? Abstraction w.r.t. a car is fine, but the encapsulation stinks. Also try to read something on 'design patterns' where abstraction is used by, say, the Facade pattern and encapsulation is used by the Decorator pattern. (but patterns often use more than one of these concepts).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Abstraction and Encapsulation

    Quote Originally Posted by JosAH View Post
    Have you read my reply #7? Abstraction w.r.t. a car is fine, but the encapsulation stinks. Also try to read something on 'design patterns' where abstraction is used by, say, the Facade pattern and encapsulation is used by the Decorator pattern. (but patterns often use more than one of these concepts).

    kind regards,

    Jos
    So you disagree with the below?

    In the context of this discussion, I tend to think of abstraction as viewing a working system or device as a black box and not caring about the implementation details (nor even aware of them). I think of encapsulation as the act of hiding the details of how the black box functions. So encapsulation is the means to enforce a consistent abstract view of some system.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Abstraction and Encapsulation

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    So you disagree with the below?
    No.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,733
    Rep Power
    5

    Default Re: Abstraction and Encapsulation

    I don't disagree with that either but I find the sentence "So encapsulation is the means to enforce a consistent abstract view of some system" too misleading due to the usage of the word "abstract". Just put it in plain English: using encapsulation you prevent direct access to the properties of a class. You can then discuss what the benefits of that design choice are.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 09-05-2012, 05:36 AM
  2. Help with abstraction
    By Shephard in forum New To Java
    Replies: 5
    Last Post: 03-05-2011, 09:43 PM
  3. Are abstraction and encapsulation concepts not in C?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 02:23 PM
  4. Abstraction
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-04-2010, 06:26 PM
  5. Abstraction
    By anurag in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •