-
November 21st, 2007, 06:36 PM
#1
Abstract VS Encapsulation
Hi guys,
What is the difference betweeb Abstract And Encapsulation?
As far as I understood Abstraction relates to the idea of hiding data that are not needed for presentation.
Is that not the definition for Encapsulation?
Or I've got it all wrong?
Thank you for your help
-
November 21st, 2007, 09:44 PM
#2
Re: Abstract VS Encapsulation
From what I understand, abstraction and encapsulation are very much related. Abstraction involves some kind of hiding, or even just ignoring, of details (e.g., turning a set of related statements into a function, grouping a set of related functions into a class, creating a hierarchy out of a set of classes, thus inheritance is a kind of abstraction). Encapsulation is more specifically data hiding, or providing a set of functions such that the underlying data (and related private functions) that is the implementation detail of this set of functions is hidden or at least unavailable for direct access.
-
November 22nd, 2007, 05:37 AM
#3
Re: Abstract VS Encapsulation
Abstraction is the process of hiding implementation detail by specifying behaviour only and leaving it to derived classes to decide how the behaviour will actually be implemented. From this we get polymorphic behaviour
Encapsulation is also about hiding implementation detail (sort of...) by making it inaccessible to calling code.
In both cases, the benefit is that calling code is restricted in its dependency on implementation detail, so is more robust in the face of change.
You can think of abstraction as being loosely allied to the IS-A relationship and encapsulation to the USES relationship.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 22nd, 2007, 09:45 AM
#4
Re: Abstract VS Encapsulation
I liked one of the articles by Andrew koening on abstraction. He said abstraction is "selective ignorance". You choose to ignore the details of implementation be it in context of exposing interfaces/abstract classes in OOP or just plain functions or files or whatever that helps you categorize various components into independent entities that can take up some responsibility to add up to provide the complete behaviour expected of a system. It is above taking up a responsibility by a module/component/class/structure/function in itself and ignoring the details of how it will be actually implemented or implementation of any relationship/dependencies with other entities. Just that component and the functionality that it would provide. For example, when you are reading this post of mine, you are just reading and probably thinking about what I have said and not about where I am from (well, you might have started to think that now... <pause> well, now, for sure) or where did I send this post from, how codeguru works and what not. You just ignore those things when you are reading this post. That is abstract thinking and your mind is applying that abstraction.
In technology, when designing applications, it is not always possible that you think that way. The thinking becomes dirty by worrying about too many details all at the same time which might be needed but not for the specific task you want to accomplish. This is all part of the divide and rule policy that helps divide a particular task into contributing components and integrating them to build up the whole thing. That is what is abstraction. Not really about hiding the implementation details but ignoring them. The hiding is what is called encapsulation. In C, that could be done by typedefs, file scope entities (using static). In C++ and other OOP supporting languages using private/protected/public/internal/default access specifiers. Encapsulation is about hiding unncessary details that might change or might cause strong coupling between entities or the details which are not really required by other components to achieve what they take ownership of; which is never found to be beneficial.
Hope this helps.
Last edited by exterminator; November 22nd, 2007 at 09:50 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
November 22nd, 2007, 10:00 AM
#5
Re: Abstract VS Encapsulation
Abstraction is the process of taking a concept or idea, and being able to turn that concept from an idea into code. Bring alive your ideas from your mind, to paper, to source code, to exectuable code is abstraction.
Encapsulation is the process of organizing your data structures and classes to get the job done. Use common sense: "The project is entirely encapsulated in one module."
The correct term for hiding implementation, is called black boxing. That is, the process of exposing only a certain set of interfaces to the client, and hiding the implementation details from the client as well.
-
November 22nd, 2007, 10:06 AM
#6
Re: Abstract VS Encapsulation
See this thread as well - abstraction. There the Leg and LivingThing are few examples of abstractions in the sense of abstract classes/concepts. The concrete classes form an abstraction in a sense as well in the context of the problem that is being solved by that application. The interfaces there are not very meaningful but should give you a good idea about what is being tried out.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
November 23rd, 2007, 08:08 AM
#7
Re: Abstract VS Encapsulation
-
January 21st, 2009, 10:28 PM
#8
Re: Abstract VS Encapsulation
Data Abstraction vs Encapsulation:
Data Abstraction increases the power of a programming language by creating user defined
data types. Using an OOP Language, one can abstract the details of a real-life entity and
hence forth simplify the implementation of a project by taking care of what the real-life
entity is supposed to do and how it can be used by the other entities without actually going
into the details of how the entity needs to be implemented.
In the above explanation, I refer to an object as a real-life entity. And objects serve the
purpose of abstracting the details of the implementation in the context of a project
implementation.
Encapsulation - is more to do with implementation details of how the abstraction is
fulfilled.
The class struct and the object instance together make encapsulation and data abstraction possible in an OOP language.
In short , abstraction ~ specification of what the object/class is supposed to do.
and encapsulation ~ is how to achieve it.
Regards,
Advika
-
January 21st, 2009, 11:52 PM
#9
Re: Abstract VS Encapsulation
Originally Posted by Salvadoravi
What is the difference betweeb Abstract And Encapsulation?
When you encapsulate something you get an inside and an outside. The outside is called (data) abstraction and the inside is called information hiding.
In practical programming terms the major mechanism for encapsulation is the class. The data abstraction is the public interface of the class the user sees (also called the type), and the hidden information is the non-public implementation of the class the user cannot see.
Last edited by _uj; January 22nd, 2009 at 12:58 AM.
-
January 22nd, 2009, 12:14 AM
#10
Re: Abstract VS Encapsulation
My view is that encapsulation is one of the means of achieving abstraction. Abstraction is simply a matter of reducing a complex task to a simple, logical set of commands (object public methods, for instance).
-
January 22nd, 2009, 12:35 AM
#11
Re: Abstract VS Encapsulation
For me abstraction means finding commonality in generality. Like vehicle is the generality and one commonality to vehicles is the ability to move. Another commonality would be refuel. This allows client code to deal in vehicles generally and tell them to move and to refuel but the client code is unaware of how the moving and refueling is taking place. Which brings us to encapsulation. Encapsulation is taking the specific implementations of the commonalities in generality and hiding them away from client code.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
January 22nd, 2009, 06:03 AM
#12
Re: Abstract VS Encapsulation
Abstraction is the idea that the class works in the way you want it to work. Encapsulation hides all the nasty translation of that abstraction into the bits you have to use to do it.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 22nd, 2009, 07:12 AM
#13
Re: Abstract VS Encapsulation
Is it the second round now?
Well, nice try everybody but here comes the definite definitions (used in OO programming):
* Encapsulation is the idea of separating type and implementation from each other so they can be considered separately. The type is the outside view of an encapsulated entity. Hidden inside lurks the implementation, a specific realization of the type.
* Abstraction is the process of defining a type. The type is used to declare variables which hold data and therefore a type is also called a data abstraction.
Last edited by _uj; January 22nd, 2009 at 08:06 AM.
-
January 22nd, 2009, 08:08 AM
#14
Re: Abstract VS Encapsulation
Originally Posted by _uj
Is it the second round now?
Who won the first round?
Originally Posted by _uj
here comes the definite definitions (used in OO programming):
As much as I like your definitions, that sounds like a bold claim. On what grounds do you say that they are "the definite definitions" with respect to OO programming instead of merely being your definitions?
-
January 22nd, 2009, 05:22 PM
#15
Re: Abstract VS Encapsulation
Originally Posted by JamesSchumacher
Abstraction is the process of taking a concept or idea, and being able to turn that concept from an idea into code. Bring alive your ideas from your mind, to paper, to source code, to exectuable code is abstraction.
Encapsulation is the process of organizing your data structures and classes to get the job done. Use common sense: "The project is entirely encapsulated in one module."
The correct term for hiding implementation, is called black boxing. That is, the process of exposing only a certain set of interfaces to the client, and hiding the implementation details from the client as well.
And yes I know that the ITERATOR code is incorrect, that's supposed to be the general iterator, I got in a hurry!
I have debugging the code I am working on as I work on it, and since I just started declaring the tree stuff, I haven't implemented it, so no debugging. For some reason I made a general iterator (implementation wise) when I implemented the tree iterator. =/
Code:
/////////////////////////////////////////////
/// (C) 2009 James Schumacher
/////////////////////////////////////////////
#ifndef CORE_COLLECTIONS_HPP_INCLUDED
#define CORE_COLLECTIONS_HPP_INCLUDED
#include <pshpack1.h>
namespace CppCore
{
namespace Collections
{
template <typename Type> class Vector
{
public:
/// Constants
enum ValueConstants
{
DefaultBlockSize = 64
};
Vector();
Vector(unsigned long dwCapacity,unsigned long dwBlockSize);
Vector(const Type * pArrayData,unsigned long dwStart,unsigned long dwCount);
Vector(const Vector<Type> & arData);
virtual ~Vector();
/// Member Functions
inline unsigned long GetCapacity() const { return m_dwCapacity; }
inline unsigned long GetBlockSize() const { return m_dwBlockSize; }
inline unsigned long GetSize() const { return m_dwCount; }
inline const Type * GetData() const { return m_pArrayData; }
inline Type * GetBuffer() { return m_pArrayData; }
inline void ReleaseBuffer(unsigned long dwCount) { m_dwCount = dwCount; }
inline void Clear() { m_dwCount = 0; }
bool SetCapacity(unsigned long dwCapacity,bool bKeepData);
/// Overloaded Operators
Vector<Type> & operator = (const Vector<Type> & arData);
inline const Type & operator [] (unsigned long dwIndex) const { return m_pArrayData[dwIndex]; }
inline Type & operator [] (unsigned long dwIndex) { return m_pArrayData[dwIndex]; }
protected:
/// Member Variables
Type * m_pArrayData;
unsigned long m_dwCapacity;
mutable unsigned long m_dwBlockSize;
unsigned long m_dwCount;
/// Member Functions
unsigned long DetermineCapacity(unsigned long dwCount) const;
};
/////////////////////////////////////
/// NOTE: NodeType for List
/// Must have two member variables
/// in NodeType of pointers to
/// to a NodeType, they must be
/// named NextListNode and
/// PreviousListNode
/// See the default base
/// list type for an example.
/////////////////////////////////////
template <typename Type> class ValueListNode
{
public:
inline ValueListNode() : NextListNode(0),PreviousListNode(0),m_udtValue() {}
inline ValueListNode(const ValueListNode<Type> & udtValue) : NextListNode(0),PreviousListNode(0),
m_udtValue(udtValue.m_udtValue) {}
inline virtual ~ValueListNode() {}
inline const Type & GetValue() const { return m_udtValue; }
inline Type & GetValueReference() { return m_udtValue; }
inline void SetValue(const Type & udtValue) { m_udtValue = udtValue; }
inline ValueListNode<Type> & operator = (const ValueListNode<Type> & arData)
{
if (this != &arData)
{
m_udtValue = arData.m_udtValue;
}
return *this;
}
public:
ValueListNode<Type> * NextListNode;
ValueListNode<Type> * PreviousListNode;
protected:
Type m_udtValue;
};
template <typename NodeType> class List
{
public:
List();
List(const List<NodeType> & udtList);
virtual ~List();
void Add(NodeType * pNode);
void Remove(NodeType * pNode,bool bDelete);
void Clear();
inline const NodeType * GetStart() const { return m_pStart; }
inline NodeType * GetStart() { return m_pStart; }
inline const NodeType * GetEnd() const { return m_pEnd; }
inline NodeType * GetEnd() { return m_pEnd; }
inline unsigned long GetSize() const { return m_dwCount; }
inline bool WillDeleteNodes() const { return m_bDeleteNodes; }
inline void SetDeleteNodes(bool bDelete) { m_bDeleteNodes = bDelete; }
List<NodeType> & operator = (const List<NodeType> & udtList);
protected:
NodeType * m_pStart;
NodeType * m_pEnd;
unsigned long m_dwCount;
bool m_bDeleteNodes;
};
template <typename Type> class IComparer
{
public:
IComparer() {}
virtual ~IComparer() {}
virtual long Compare(const Type & obj1,const Type & obj2) const = 0;
virtual bool Equal(const Type & obj1,const Type & obj2) const = 0;
virtual bool NotEqual(const Type & obj1,const Type & obj2) const = 0;
};
template <typename Type> class Comparer : public IComparer<Type>
{
public:
enum ErrorCodes
{
CompareError = -15
};
Comparer() {}
virtual ~Comparer() {}
virtual long Compare(const Type & obj1,const Type & obj2) const
{
long nRetValue = CompareError;
if (obj1 < obj2)
{
nRetValue = -1;
}
else if (obj1 > obj2)
{
nRetValue = 1;
}
else if (obj1 == obj2)
{
nRetValue = 0;
}
return nRetValue;
}
virtual bool Equal(const Type & obj1,const Type & obj2) const
{
return obj1 == obj2;
}
virtual bool NotEqual(const Type & obj1,const Type & obj2) const
{
return obj1 != obj2;
}
};
template <typename UDTValue> class ValueTreeNode : public ValueListNode<UDTValue>
{
public:
typedef ValueTreeNode<UDTValue> NodeType;
inline ValueTreeNode() : ValueListNode<UDTValue>(),ParentTreeNode(0),
LessTreeNode(0),GreaterTreeNode(0),EqualList(),
TreePriorNode(0),NextNode(0),DeleteMe(true) {}
inline virtual ~ValueTreeNode() {}
/// Member Variables
NodeType * ParentTreeNode;
NodeType * LessTreeNode;
NodeType * GreaterTreeNode;
List<UDTValue> EqualList;
NodeType * TreePriorNode;
NodeType * NextNode;
bool DeleteMe;
};
template <typename NodeType> class TreeNodeIter
{
public:
TreeNodeIter() : m_pBase(0),m_dwOffset(0) {}
TreeNodeIter(const NodeType * pNode) : m_pBase(const_cast<NodeType *>(pNode)),m_dwOffset(0) {}
TreeNodeIter(const TreeNodeIter<NodeType> & obj) : m_pBase(obj.m_pBase),m_dwOffset(obj.m_dwOffset) {}
~TreeNodeIter() {}
inline void Reset()
{
m_dwOffset = 0;
}
inline const TreeNodeIter<NodeType> & operator ++ ()
{
++m_dwOffset;
return *this;
}
inline TreeNodeIter<NodeType> operator ++ (int nDummy)
{
TreeNodeIter<NodeType> retValue(*this);
++m_dwOffset;
return retValue;
}
inline const TreeNodeIter<NodeType> & operator -- ()
{
--m_dwOffset;
return *this;
}
inline TreeNodeIter<NodeType> operator -- (int nDummy)
{
TreeNodeIter<NodeType> retValue(*this);
--m_dwOffset;
return retValue;
}
inline const NodeType & operator * () const
{
return *(m_pBase + m_dwOffset);
}
inline NodeType & operator * ()
{
return *(m_pBase + m_dwOffset);
}
inline const TreeNodeIter<NodeType> & operator = (const TreeNodeIter<NodeType> & udtIter)
{
if (this != &udtIter)
{
m_pBase = udtIter.m_pBase;
m_dwOffset = udtIter.m_dwOffset;
}
return *this;
}
inline const TreeNodeIter<NodeType> & operator = (const NodeType * pNode)
{
m_pBase = const_cast<NodeType *>(pNode);
m_dwOffset = 0;
return *this;
}
inline TreeNodeIter<NodeType> & operator += (unsigned long nValue)
{
m_dwOffset += nValue;
return *this;
}
inline TreeNodeIter<NodeType> & operator -= (unsigned long nValue)
{
m_dwOffset -= nValue;
return *this;
}
inline TreeNodeIter<NodeType> operator + (unsigned long nValue) const
{
TreeNodeIter<NodeType> udtRetValue(*this);
udtRetValue.m_dwOffset += nValue;
return udtRetValue;
}
inline TreeNodeIter<NodeType> operator - (unsigned long nValue) const
{
TreeNodeIter<NodeType> udtRetValue(*this);
udtRetValue.m_dwOffset -= nValue;
return udtRetValue;
}
private:
NodeType * m_pBase;
unsigned long m_dwOffset;
};
///////////////////////////////////////////////////
/// Note for tree nodes
/// You don't have to use the above node type (or as a base)
/// value tree node, however, you have to atleast
/// use the same naming conventions that
/// encapsulate the same functionality
///////////////////////////////////////////////////
template <typename NodeType> class Tree
{
public:
Tree();
virtual ~Tree();
/// Member Functions
bool Add(NodeType * pNode);
bool Add(NodeType * pNode,const IComparer<NodeType> * pComparer);
void Clear();
TreeNodeIter<NodeType> * GetIterator() const; /// NOTE: Should change this to return an object and not a pointer!
protected:
/// Member Variables
NodeType * m_pRootNode;
NodeType * m_pHighNode; /// Highest priority node
NodeType * m_pLowNode; /// Lowest priority node
unsigned long m_dwNodeCount; /// How many nodes are in the tree (base nodes, doesn't count equal nodes)
};
}
}
#include <poppack.h>
#include "Core.Vector.hpp"
#include "Core.List.hpp"
#include "Core.Tree.hpp"
#endif // CORE_COLLECTIONS_HPP_INCLUDED
Last edited by JamesSchumacher; January 22nd, 2009 at 05:37 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|