Jump to content

Abstraction in object-oriented programming

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 24.150.61.63 (talk) at 16:29, 9 April 2002 (on strictly technical grounds 24 is almost always correct - while LDC is confusing and uniformed). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.

When abstraction proceeds into the operations defined, enabling objects of different types to be substituted, it is called polymorphism. When it proceeds in the opposite direction, inside the types or classes, structuring them to simplify a complex set of relationships, it is called delegation or inheritance. 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, for example, there is less of a class-instance distinction, more use of generics to achieve abstraction and polymorphism and individual objects and functions are abstracted more flexibly to better fit with LISP's functional heritage.

In the Java, however, abstraction is achieved most commonly with an extended data type called a class, while objects of that type are called instances of that class. Other languages facilitate more complex abstractions, and relying overmuch on Java terminology is quite misleading, but Java examples proliferating on the Internet tend to be one of the easiest ways to understand how abstraction applies in different domains.

Here is a sample Java fragment to represent some common farm "animals" to a level of abstraction suitable to model some 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);

Lots more to say here about how different languages deal with abstraction, etc...