Lecture 3: Data Abstraction & Encapsulation

Computer Programming

J Mwaura

Data Abstraction

Data Abstraction - provide only the essential details to the outside world & hide the internal details

This technique separates the interface & implementation details of the program

Data Abstraction can be achieved in two ways:

  • Abstraction using classes & objects
  • Abstraction in header files

Abstraction using classes & objects

Using classes, you decide which data member or member function are to be accessible outside the class or not

This is done using the access specifiers (public, private or protected)

Data Abstraction: Classes

Abstraction in header files

Header files indicate the inclusion of several inbuilt library functions associated with that header file

For instance, if we include the header file #include e.g. string.h, math.h, functions such as strlen(), strcpy and many more are accessible to the programmer without getting to know the internal logic behind these inbuilt functions

Data Abstraction: Header files

Encapsulation

This is the 1st principle of Object Oriented Programming

To wrap up data & information under a single unit

In OOP, encapsulation is to bind together data & functions that manipulates the data

We use access specifiers in implementing encapsulation in C++

Encapsulation

A class is a data structure contain both variables or (members) & functions or (methods) in a single entity

Access to class members from outside the class is controlled by access specifiers

  • These "deny access to the variable members" but allow access to methods that can store and retrieve data from those variable members

This technique of "data hiding" ensures that stored data is safely confined within the class variable members

Encapsulation

Access specifier uses keywords as; public, private, or protected to specify access rights for its listed members;

  • Public members are accessible from any place where the class is visible
  • Private members are accessible only to other members of the same class
  • Protected members are accessible only to other members of the same class & to members of classes derived from that class

By default, all class members have private access

Encapsulation

data information

Encapsulation

data information

Encapsulation

Creating an object

  • In order to assign and retrieve data from private members of a class, special public accessor methods must be added to the class.
    1. Setter methods, - to assign data
    2. Getter methods, - to retrieve data
  • Accessor methods are often named as the variable they address, with the first letter made uppercase, and prefixed by set or get respectively
    1. For example, accessor methods to address an age variable may be named setAge() & getAge()

Encapsulation

What's the difference?

Data Abstraction Data Encapsulation
Solves access issues by proposing a design Solves access issues by implementing the design proposed by data abstraction
Hides the background details & explanation of how the data is derived Hides the data by combining it with functions & objects as a single unity to protect it from unauthorized users
Focuses on what the object does Hides the internal details by giving it restricted access
Abstract by default Abstract data types - as they are used to create objects of its own type

End of Lecture 3

Computer Programming

That's it!

Queries about this Lesson, please send them to: jmwaura.uni@gmail.com

*References*

  • C++ Programming - An Object-Oriented Approach, 2019 Behrouz, Richard et.al
  • Accelerated C++ - Practical Programming by Example, 2000 Andrew, Barbara et.al
Courtesy of
Computer Programming