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.
The simplest form of it extends the concept of data type from earlier programming languages to associate behavior more strongly with the data. This is done to achieve encapsulation and a limited degree of polymorphism. These terms are very often used in contradicatory ways by users of various object-oriented progamming languages, which offer similar facilities for abstraction.
For example, Linda abstracts the concepts of server and shared data-space to facilitate distributed programming. In CLOS or self, for example, there is less of a class-instance distinction, more use of delegation for polymorphism, and individual objects and functions are abstracted more flexibly to better fit with a shared functional heritage from LISP. In Java, abstraction takes place at the level of extended data types. Such types are called a classes, and objects are instances of some class.
For example, here is a sample Java fragment to represent some common farm "animals" to a level of abstraction suitable to model simple aspects of their hunger and feeding. 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);
In the above example, the class animal is an abstraction used in place of an actual animal, LivingThing is a further abstraction (in this case a generalisation) of animal.
Lots more to say here about how different languages deal with abstraction, etc...