Geeks Talk

Prepare for your Next Interview




abstraction Vs encapsulation

This is a discussion on abstraction Vs encapsulation within the OOPS forums, part of the Software Development category; Hi i have some confusion related to the following... Abstraction - hiding implementation. encapsulation - hiding data. Is the above definitions are correct ? If yes How these two terms are related? I ...


Go Back   Geeks Talk > Software Development > OOPS

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-16-2007
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,007
Thanks: 0
Thanked 69 Times in 58 Posts
psuresh1982 will become famous soon enough
abstraction Vs encapsulation

Hi i have some confusion related to the following...

Abstraction - hiding implementation.
encapsulation - hiding data.

Is the above definitions are correct ?
If yes How these two terms are related? I mean can these things exist without each other?

------------------------
suresh
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2007
Junior Member
 
Join Date: Jul 2007
Location: india
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
sushantl is on a distinguished road
Thumbs up Re: abstraction Vs encapsulation

Encapsulation has two faces; data abstraction and information hiding. Data abstraction is a type seen from the outside. Information hiding is a type seen from the inside.

Sometime encapsulation is used to mean information hiding only but I think the definition I gave is better because if you encapsulate something you get both an inside and an outside right.

* Abstraction focuses on the outside view of an object (i.e. the interface)



* Encapsulation (information hiding ) prevents clients from seeing its inside view, where the behavior of the abstraction is implemented
Reply With Quote
  #3 (permalink)  
Old 07-20-2007
Expert Member
 
Join Date: Jul 2007
Location: Kolkata
Posts: 110
Thanks: 6
Thanked 6 Times in 6 Posts
animesh.chatterjee is on a distinguished road
abstraction vs encapsulation

Abstraction uses in order to do some works easierly. For example, you have a abstract class fruit. And fruit has some methods one of which has to be abstract . So you should fill the body of this method's body for your each class who are subclass of this abstract class.THe purpoose of it is that to implement method according to that class.

Abstraction is the process of hiding the details and exposing only the essential features of a particular concept or object.
Programming is managing complexity. whenwe uses abstraction as a tool for managing complexity in our java pro..

Encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions). Programs written in older languages did not enforce any property/method relationship. This often resulted in side effects where variables had their contents changed or reused in unexpected ways and 'spaghetti' code (branching into procedures from external points) that was difficult to unravel, understand and maintain. Encapsulation is one of three fundamental principles within object oriented programming languages.

Data hiding is the ability of objects to shield variables from external access. These private variables can only be seen or modified by use of object accessor and mutator methods. This permits validity checking at run time. Access to other object variables can be allowed but with tight control on how it is done. Methods can also be completely hidden from external use. Those that are made visible externally can only be called by using the object's front door (ie. there is no 'goto' branching concept).

public class Box
{
// what are the properties or fields
private int length;
private int width;
private int height;

// what are the actions or methods
public void setLength(int p)
{length = p;}

public void setWidth(int p)
{width = p;}

public void setHeight(int p)
{height = p;}

public int displayVolume()
{System.out.println(length*width*height);}
}

let me know if you need anymore help
Reply With Quote
  #4 (permalink)  
Old 07-27-2007
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,330
Thanks: 7
Thanked 117 Times in 106 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: abstraction Vs encapsulation

Some of the threads are merged for better management of the site.

MODERATOR
Reply With Quote
  #5 (permalink)  
Old 09-28-2007
Junior Member
 
Join Date: Sep 2007
Location: indore
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ajay_golani is on a distinguished road
Re: abstraction Vs encapsulation

encapsulation:- it means binding up of data and methods it means the concept of class in which we put together the data members and methods or function members.
abstraction:- it means that hiding the background details which are not useful to the user for it we use normaly private or protected access specifier.
by it data is not let free to move around the system.
Reply With Quote
  #6 (permalink)  
Old 05-25-2008
Junior Member
 
Join Date: Jun 2007
Location: Redmond, WA, USA
Posts: 8
Thanks: 0
Thanked 2 Times in 1 Post
SomGollakota is on a distinguished road
Re: abstraction Vs encapsulation

Quote:
Originally Posted by psuresh1982 View Post
Hi i have some confusion related to the following...

Abstraction - hiding implementation.
encapsulation - hiding data.

Is the above definitions are correct ?
If yes How these two terms are related? I mean can these things exist without each other?

------------------------
suresh
Encapsulation: A simple definition of encapsulation is - combining the data (information) and the methods (functions) that can manipulate that data into one capsule (class/object). Depending on how you write program encapsulation, it guarantees that the encapsulated data is not accessed by any other function/method/program outside the encapsulated object. For example,
class MyCapsule
{
private:
int myInt;
char myChar;
public:
MyIntFunc() { myInt = 10; }
MyCharFunc() { myChar = 'A'};
};

In this case, no other program/function/method can access myInt other than MyIntFunc. Same is true for myChar and MyCharFunc.

Abstraction: A simple definition of abstraction is to hide actual implementation of an object from the external world that would use the object. For example, a program that is drawing circles and squares using those objects need not know how those objects are implemented. It is enough for the program to know what the behavior of these objects is, and how to use these objects (rather than how these objects are implemented internally).

So, drawing a parallel between abstraction and encapsulation, when you encapsulate data and methods that operate on data into one object, the external program that uses this object need not know the internal workings of the object to use the object. Thus making the object abstract data type to the external program. Classic examples of abstract data type in C (yes) are int, char, float, double etc. Classes are OOPL variations and extensions of the traditional abstract data types.
------------------------------
Reply With Quote
  #7 (permalink)  
Old 07-09-2008
Junior Member
 
Join Date: Jul 2008
Location: Faridabad-Haryana-India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
rahulsharma347 is on a distinguished road
Re: abstraction Vs encapsulation

Abstraction means hiding the internal details and just exposing the functionality. For Example:-When you change the gear of your car, you know the gears will be changed without knowing how they are functioning internally.Abstraction focuses on the outside view of an object (i.e. the interface)

Encapsulation means put the data and the function that operate on that data in a single unit(information hiding) .Encapsulation prevents clients from seeing its inside view, where the behavior of the abstraction is implemented.
Reply With Quote
  #8 (permalink)  
Old 07-16-2008
Junior Member
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Deepyog is on a distinguished road
Re: abstraction Vs encapsulation

hi dear...
Encapsulation is the process of hiding data member and its functionality on data members.
Abstraction is the interface which hides the inner functionality.
Let me explain you with an example...
Lets say there is switch button.. We know that if press it on... it will work if i press it off.. it stops work.. the switch which is given to you is an abstraction as it is hiding inner details... and wires inside the switch and how there wires are working... all are hidden from us.. is an encapsulation.
Reply With Quote
Reply

  Geeks Talk > Software Development > OOPS


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
C++ Abstraction An Introduction Lokesh M C and C++ 0 04-26-2007 11:52 PM
achieve Encapsulation in Java JobHelper Java 1 12-27-2006 08:39 AM


All times are GMT -4. The time now is 06:25 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved