![]() |
|
![]() |
| LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Junior Member Join Date: Jul 2009 Age: 23
Posts: 12
Thanks: 0
Thanked 4 Times in 2 Posts
Rep Power: 1 | difference between abstraction and encapsulation? difference between abstraction and encapsulation? I want know the exact diff between abstration and encapsulation. I heard from sources Abstraction means hiding the details and showing only the essential features. Encapsulation means bundling of data with methods. I just need clear cut example. |
| | |
| | #2 (permalink) |
| Moderator Join Date: Jul 2006
Posts: 2,191
Thanks: 5
Thanked 505 Times in 324 Posts
Rep Power: 73 | Re: difference between abstraction and encapsulation? Encapsulation is hiding the implementation details which may or may not be for generic or specialized behavior(s). Abstraction is providing a generalization (say, over a set of behaviors). * Abstraction lets you focus on what the object does instead of how it does it * Encapsulation means hiding the internal details or mechanics of how an object does something. Like when you drive a car, you know what the gas pedal does but you may not know the process behind it because it is encapsulated. Let me give an example in C#. Suppose you have an integer: int Number = 5; string aStrNumber = Number.ToString(); you can use a method like Number.ToString() which returns you characters representation of the number 5, and stores that in a string object. The method tells you what it does instead of how it does it. encapsulation puts some things in a box and gives you a peephole; this keeps you from mucking with the gears. abstraction flat-out ignores the details that don't matter, like whether the things have gears, ratchets, flywheels, or nuclear cores; they just "go" Another example: Suppose I created an immutable Rectangle class like this: class Rectangle { public: Rectangle(int width, int height) : width_(width), height_(height) {} int width() const { return width_; } int height() const { return height_; } private: int width_; int height_; } Now it's obvious that I've encapsulated width and height (access is somehow restricted), but I've not abstracted anything (okay, maybe I've ignored where the rectangle is located in the coordinates space, but this is a flaw of the example). Good abstraction usually implies good encapsulation. An example of good abstraction is a generic database connection class. Its public interface is database-agnostic, and is very simple, yet allows me to do what I want with the connection. And you see? There's also encapsulation there, because the class must have all the low-level handles and calls inside. Encapsulation: Is hiding unwanted/un-expected/propriety implementation details from the actual users of object. e.g. List list.Sort(); /* Here, which sorting algorithm is used and hows its implemented is not useful to the user who wants to perform sort, that's why its hidden from the user of list. */ Abstraction: Is a way of providing generalization and hence a common way to work with objects of vast diversity. e.g. class Aeroplane : IFlyable, IFuelable, IMachine { // Aeroplane's Design says: // Aeroplane is a flying object // Aeroplane can be fueled // Aeroplane is a Machine } // But the code related to Pilot, or Driver of Aeroplane is not bothered // about Machine or Fuel. Hence, // pilot code: IFlyable flyingObj = new Aeroplane(); flyingObj.Fly(); // fighter Pilot related code IFlyable flyingObj2 = new FighterAeroplane(); flyingObj2.Fly(); // UFO related code IFlyable ufoObj = new UFO(); ufoObj.Fly(); // **All the 3 Above codes are genaralized using IFlyable, // Interface Abstraction** // Fly related code knows how to fly, irrespective of the type of // flying object they are. // Similarly, Fuel related code: // Fueling an Aeroplane IFuelable fuelableObj = new Aeroplane(); fuelableObj.FillFuel(); // Fueling a Car IFuelable fuelableObj2 = new Car(); // class Car : IFuelable { } fuelableObj2.FillFuel(); // ** Fueling code does not need know what kind of vehicle it is, so far // as it can Fill Fuel** 0 vote down Encapsulation require modularity. It requires you to create objects that has the data and the methods to process the data. In this case you can view it as a module. Abstraction provides you a generalized view of your classes. 0 vote down A priori, they've got nothing in common. Most answers here focus on OOP but encapsulation begins much earlier; every method is an encapsulation: point x = { 1, 4 }; point y = { 23, 42 }; int d = distance(x, y); Here, distance encapsulates the calculation of the (euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple. Abstraction is the process of generalization: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C's qsort function which sorts data. The thing about qsort is that it doesn't care about the data it sorts – in fact, it doesn't know what data it sorts. Rather, its input type is a typeless pointer (void*) which is just C's way of saying “I don't care about the type of data” (this is also called type erasure). The important point is that the implementation of qsort always stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type. qsort therefore expects the user to provide said compare function as a function argument. They're similar and both deal with hiding complexity, right? Here's the view I like... Encapsulation is an internal hiding of complexity that entails breaking things into simple, well-named functions, and other means of simplifying code from an internal perspective. Abstraction is the external hiding of that complexity. This is largely accomplished through hiding internal methods and only exposing the minimum set of tools the user needs to accomplish the task.
__________________ Kiran |
| | |
| | #3 (permalink) |
| Junior Member Join Date: Sep 2006 Location: Hyderad Age: 27
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 4 | Re: difference between abstraction and encapsulation? Hi The easier way to understand Abstraction and encapsulation is as follows Think like Abstraction = Question Encapsulation = Answer means what ever questions you get about the problem will become Abstraction and How you are going to solve will be the encapsulation Take an example of Car All the questions you get about car is Abstraction what is car ?, what is it's color? , what engine?,how many wheels? The Answers to Question become the Encapsulation and with out Abstraction (Question) there is no Encapsulation(Answer) hope this explanation helps you to understand. Cheers Vishnu |
| | |
![]() |
| Tags |
| abstraction , difference , encapsulation |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Abstraction and Encapsulation in Java? | radhika743 | JAVA Technologies | 2 | 08-02-08 04:44 PM |
| TCS Pacement paper 29 | sridhar | TCS Placement Papers | 0 | 12-02-07 05:43 PM |
| J2EE-What is the difference between encapsulation & abstraction? | preethisingh | Java Interview / Technical Questions | 1 | 24-01-07 06:50 PM |
| More Interview Questions Here... |