Abstraction in object-oriented programming
In object-oriented programming theory, abstraction is the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system.
Various object-oriented progamming languages offer similar facilities for abstraction. In Java, for example, abstraction is achieved by extending the concept of data type from earlier programming languages to include not only state information (i.e., data) but also behavior (i.e. procedures). This extended data type is called a class, and objects of that type are called instances of that class. Other languages may facilitate more complex abstractions. For example, Linda abstracts the concepts of server and shared data-space to facilitate distributed programming. In CLOS, for example, there is less of a class-instance distinction, and individual objects and functions are abstracted more flexibly to better fit with LISP's procedural heritage.
Here a sample Java fragment to represent "animals". It defines an Animal class to represent both the state of the animal and its functions:
class Animal extends LivingThing { Location m_loc; double m_energy_reserves; boolean is_hungry() { if (m_energy_reserves < 2.5) { return true; } else { return false; } } void eat(Food f) { // Consume food m_energy_reserves += f.getCalories(); } void moveto(Location l) { // Move to new location m_loc = l; } }
With the above definition, one could create objects of type Animal and call their methods like this:
thePig = new Animal(); theCow = new Animal(); if (thePig.is_hungry()) { thePig.eat(table_scraps); } if (theCow.is_hungry()) { theCow.eat(grass); } theCow.move(theBarn);
Lots more to say here about how different languages deal with abstraction, etc...