Encyclopedia > Abstraction in object-oriented programming

  Article Content

Abstraction (computer science)

Redirected from Abstraction in object-oriented programming

In computer science, abstraction is a technique to factor out details and ease use of code and data. It is by analogy with abstraction[?] in mathematics[?].

Control abstraction, seen in structured programming, is use of subprograms and control flows[?]. Data abstraction is primary motivation of introducing datatype and subsequently abstract data types.

Object-oriented programming can be seen as an attemp to abstract both data and code.

Examples are lambda abstractions (making a term into a function of some variable), higher-order functions (parameters are functions), bracket abstraction[?] (making a term into a function of a variable).

The opposite abstraction is concretisation[?].

This article (or an earlier version of it) contains material from FOLDOC, used with permission. Modify if needed.

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.

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...



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Digital Rights Management

... protection" systems: Serial copy management system[?] (SCMS) Macrovision iTunes Windows media player[?] DRM and document restriction technology Opponents ...

 
 
 
This page was created in 31.4 ms