Prepare for your Next Interview
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 ...
|
|||
|
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 |
| Sponsored Links |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
Re: abstraction Vs encapsulation
Quote:
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. ------------------------------ |
|
|||
|
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. |
|
|||
|
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. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |