Programming Language Concept

Just another Binusian blog site

Session 8 : Abstract Data Types

December8

The Concept of Abstraction

  • An abstraction is a view or representation of an entity that includes only the most significant attributes
  • The concept of abstraction is fundamental in programming (and computer science)
  • Nearly all programming languages support process abstraction with subprograms
  • Nearly all programming languages designed since 1980 support data abstraction

Introduction to Data Abstraction

  • An abstract data type is a user-defined data type that satisfies the following two conditions:

–The representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type’s definition

–The declarations of the type and the protocols of the operations on objects of the type are contained in a single syntactic unit. Other program units are allowed to create variables of the defined type.

Language Examples: C++

  • Based on C struct type and Simula 67 classes
  • The class is the encapsulation device
  • A class is a type
  • All of the class instances of a class share a single copy of the member functions
  • Each instance of a class has its own copy of the class data members
  • Instances can be static, stack dynamic, or heap dynamic
  • Information Hiding–Private clause for hidden entities

    Public clause for interface entities

    Protected clause for inheritance

  • Constructors:

    –Functions to initialize the data members of instances (they do not create the objects)

    –May also allocate storage if part of the object is heap-dynamic

    –Can include parameters to provide parameterization of the objects

    –Implicitly called when an instance is created

    –Can be explicitly called

    –Name is the same as the class name

  • Destructors

–Functions to cleanup after an instance is destroyed; usually just to reclaim heap storage

–Implicitly called when the object’s lifetime ends

–Can be explicitly called

–Name is the class name, preceded by a tilde (~)

Encapsulation Constructs

  • Large programs have two special needs:

–Some means of organization, other than simply division into subprograms

–Some means of partial compilation (compilation units that are smaller than the whole program)

  • Obvious solution: a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units)
  • Such collections are called encapsulation

Naming Encapsulations

  • Large programs define many global names; need a way to divide into logical groupings
  • A naming encapsulation is used to create a new scope for names
  • C++ Namespaces

–Can place each library in its own namespace and qualify names used outside with the namespace

–C# also includes namespaces

  • Java Packages

–Packages can contain more than one class definition; classes in a package are partial friends

–Clients of a package can use fully qualified name or use the import declaration

  • Ruby classes are name encapsulations, but Ruby also has modules
  • Typically encapsulate collections of constants and methods
  • Modules cannot be instantiated or subclassed, and they cannot define variables
  • Methods defined in a module must include the module’s name
  • Access to the contents of a module is requested with the require method

Email will not be published

Website example

Your Comment: