Object-Oriented Programming: Encapsulation

Kyle McCurley
3 min readFeb 28, 2020

--

Encapsulation is the ‘wrapping-up’ of different elements into a single unit, typically for the purposes of security. Normally, encapsulation occurs using a class to bundle up methods under a single type.

An encapsulated object only allows for outside interaction through an object’s interface. All interaction with an object is done through calling methods available within the interface.

Encapsulation serves a vital role in Object-Oriented Programming; it allows for more security of the program exposing only necessary data to the object’s public interface. This risks as little data as possible from unintentional interference.

Ruby implements encapsulation through:

Private Keywords

Public Keywords

Private:

The access scope for a private method is only limited to within a class’ scope. Meaning, a private method is innaccessible from outside a class. The only way to gain access to data within a private method is by calling it from a public method.

Access Scope: Where in a program a varaible/method can be accessed.Public Method: Method exposed from a public interface.Private Method: Method available only within a class, cutting access from outside of the class.

On line 1, the constructor is defined on lines 3–4 to assign the arguments of name and ageto the instance variables of @name and @age . These instance variables are innaccesible from outside the Dog class, since there isn’t a getter method defined on the public interface.

Protected:

A protected method is very similar to a private method, except a protected method can only be called from within a class, but not by any unrelated class.

For example:

Example of Protected Method Use

In the code above, introduce()references the getter and setter methods for the name and age variables. The accessor methods are initialized under the protected keyword, making attr_reader :name, :age protected methods.

If theory is correct, I should be able to access :name and :age within the Pet and Dog classes, with a NoMethodError thrown when I call sparky.name and sparky.age .

This code example demonstrates the behavior of protected methods: when I try to call a protected method/state within its class or derived-classes, I can. However, a protected method can not be accessed outside of its class/derived classes, doing so will raise an error.

Additionally, protected methods allow a derived-class to access the behavior of a super class that would otherwise be hidden from the public interface. This is demonstrated with introduce() accessing the protected getter methods. While these getter methods are inaccessible from the public interface, using a protected keyword allows the programmer to grant access to hidden behavior for increased program performance.

Encapsulation also describes the ‘bundling’ of data into single units. This is demonstrated with:

  • Classes: Encapulation of Methods
  • Methods: Encapsulation of Logic
  • Objects: Encapsulation of State

Classes ‘bundle’ methods into single units, methods ‘bundle’ logic into a single unit, and objects bundle ‘state’ into a single unit. Access to the data within these units can be controlled based on an object’s interface.

Interface: Logical point where objects can communicate with one another.

Summary:

This article was written to clearly articulate the concepts of Object-Oriented Programming, Encapsulation specifically. I hope this has helped you (readers) during your journeys on the road towards mastering the Art of Craft of Software Development.

Many thanks to the extremely helpful and friendly staff at Launch School for playing such a large role in that journey.

--

--