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

Encapsulation Construct

  • 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

Nested Subprograms

  • Organizing programs by nesting subprogram definitions inside the logically larger subprograms that use them
  • Nested subprograms are supported in Ada, Fortran 95+, Python, JavaScript, and Ruby

Encapsulation in C

  • Files containing one or more subprograms can be independently compiled
  • The interface is placed in a header file
  • Problem: the linker does not check types between a header and associated implementation
  • #include preprocessor specification – used to include header files in applications

Encapsulation in C++

  • Can define header and code files, similar to those of C
  • Or, classes can be used for encapsulation

–The class is used as the interface (prototypes)

–The member definitions are defined in a separate file

  • Friends provide a way to grant access to private members of a class

The danger of C’s approach to encapsulation:
There are two problems with this approach. First, the documentation of the dependence of the client program on the library (and its header file) is lost. Second, the author of the library could change the header file and the implementation file, but the client could attempt to use the new implementation file (not realizing it had changed) but with the old header file, which the user had copied into his or her client program.

Naming Encapsulation

  • 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

Similarities between Java Packages dan C++ Namespace:

1) Java Packages are namespaces system while C++ also has namespace
2) Both are abstract containers for classes
3) Java Packages and namespaces provide breakup for class names
4) both make the code cleaner and less redundant, by enabling some
parts of the code.
Difference between Java Packages and C++ Namespace:
1) Java packages are about modules meanwhile, in C++ namespaces are just about partitioning the available names.
2) In Java language, namespaces are intended to avoid conflicts between names in different parts of class libraries.
3) C++ namespaces have an extra way to encapsulate stuff within an already existing system.

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