问题
Possible Duplicate:
Abstraction VS Information Hiding VS Encapsulation
Can somebody explain to me the main differences between the principles of encapsulation and abstraction in objected-oriented programming (if possible with examples).
回答1:
Sample:
// NO ABSTRACTION, NO ENCAPSULATION
const int catLegs = 4;
const int spiderLegs = 8;
Leg[] catLegs;
Leg[] spiderLegs;
void MakeCatRun(Distance b) { for (int i=0; i<catLegs; ++i) catLegs[i] += b; }
void MakeSpiderRun(Distance b) { for (int i=0; i<spiderLegs; ++i) spiderLegs[i] += b; }
Encapsulation:
// ENCAPSULATION
class Cat
{
Leg[] legs;
int nLegs;
public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; }
}
class Spider
{
Leg[] legs;
int nLegs;
public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; }
}
Abstraction:
// ABSTRACTION
class LivingBeing
{
Leg[] legs;
int nLegs;
public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; }
}
class Cat: LivingBeing { }
class Spider: LivingBeing { }
回答2:
Abstraction is that quality that we just do not bother about unnecessary internal mechanisms (implementations) and can deal with a system/object, looking in to the essentialities.
Eg: While applying brakes on a car you just don't care if it has got air-brake or hydraulic-brake. Abstraction comes in the form of a pedal-push here.
Encapsulation is something that makes the above (abstraction) possible by packing (encapsulating) details of implementation within a container (hiding away breaking mechanisms and tiny components from your eyesight or physical contact, in the above case).
So, Encapsulation in effect provides Abstraction!
If you look around, you can see it everywhere around you in real world. Also it is there in programming- If Someone provides you a class to Sort list of integers, you don't really need to bother about the sorting algorithm (bubble sort/Quick Sort) it uses, The abstraction makes it possible that you 'pass the list of integers' to the method; that is it.
class Sorter
{
public List<Integer> Sort(List<Integer>)//Only this method is seen outside
{
String pattrenName=this.AdvancedPatternFinder();
this.Advancedsorter(pattenName);
//Return sorted list
}
private String AdvancedPatternFinder(){}//NOT seen from outside
private void Advancedsorter(String pattrenName){}//NOT seen from outside
}
See the below animation to understand how a neat abstraction is provided by encapsulating internal details inside!
image courtesy:this blog
回答3:
Encapsulation means that object's internal state (data) can be modified only by its public methods (public interface):
class Encapsulated {
private int data;
public int getData() { return data; }
public void setData(int d) { data = d; }
}
Abstraction means that you can abstract from concrete implementations, for example:
abstract class Container {
int getSize();
}
class LinkedList extends Container {
int getSize() { /* return the size */ }
}
class Vector extends Container {
int getSize() { /* ... */ }
}
If you will be using the Container abstract class in all your code instead of Vector or LinkedList, you will be able to switch the concrete implementation of Container (Vector/LinkedList in this case) without changing any of your code, thus abstracting yourself from the implementation.
回答4:
The two concepts are distinct, but closely related and are often found together.
Abstraction is hiding of non-essential details, usually termed implementation details. For example, to read data from a stream, most environments have the concept of an InputStream
. It provides a ReadBytes() method to fetch more bytes from the stream. How it does this is abstracted away - it could be reading from a file, using the OS file APIs, or reading from a socket, from SSH or anything else that can be presented as a stream of bytes. The InputStream class is said to be an abstraction.
Each implementation of InputStream codes how the bytes are read. For example, a FileInputStream reads bytes from a file, and maintains the current offset into the file as a private data member. Code external to the class does not have free access to read or write this variable directly - doing so might lead to incorrect results, such as part of the stream being skipped, or re-read. This is especially important in multi-threaded scenarios, when changes to the variable needs to be carefully controlled. Instead of giving unfettered access, the class uses the offset
internally (by declaring it private
), and gives external code only indirect access, such as via a method GetCurrentOffset()
, and methods Seek(int offset)
. The offset variable is then encapsulated by the FileInputStream class.
来源:https://stackoverflow.com/questions/3362335/difference-between-encapsulation-and-abstraction-concepts