Jump to content

Abstraction in object-oriented programming

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lee Daniel Crocker (talk | contribs) at 23:32, 20 December 2001. 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 concept of data type from earlier programming languages is extended 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.


For example, a Java program may need to represent,

say, represent animals, so it would define 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 object 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...