Encapsulation, More Than Data Hiding.

For my last blog, I revisited the important concept of polymorphism. For this post, I decided to revisit another important concept, encapsulation. I wanted to find a similarly formatted article/blog post to the one I found for polymorphism, but wanted to get a different perspective from a different resource. I wanted a source that still mixes in code examples,

Let’s start with the definition of encapsulation. Encapsulation is the ability to package data and related behaviors, in an object. This also includes the access permissions for the behaviors within the object. Encapsulation is often used a synonym for data hiding, but it’s important to acknowledge that while encapsulation includes data hiding, this isn’t all that encapsulation is. Everyone thinks of encapsulation as the concept of public versus private variable types. The concept of encapsulation does include this concept, but this is only one area that relates to encapsulation. When creating a new class, encapsulation should be a top priority. This is because with encapsulation, maintainability is drastically increased.

To further explain that encapsulation is not only data hiding, the article uses a classic coding example, an animal class. The class contains several attributes (name, type, height, color) and behaviors (hunt, run, mate, getName). Encapsulation is more than just making sure the attributes are private, it is about bundling the attributes and the related behaviors within one class. We could have the hunt, run, mate, and getName behaviors all in separate classes theoretically, but this would just be poor design. The article also notes that encapsulation is not about getters and setters. While these behaviors are related to the attributes, the authors wanted to make sure readers didn’t confuse encapsulation with this concept. Encapsulation does include getters and setters, but they are not the focus, instead we should focus on the behaviors that return a different value depending on the attribute, instead of just returning the attribute or setting a new value for the attribute.

To explain how I would use this in the future, why not explain how I would expand upon the code example used in the article? This code example in the article only includes hunt, run, mate, and getName for behaviors. If we wanted to add a speak functionality to our animal, we would use encapsulation. This might mean adding a new private variable, but also would include adding a new method that depends on an attribute or several. This behavior and additional attribute(s) would be added to our animal class to encapsulate the data and behaviors, and ensure the best maintainability for our codebase.

I found this article to be simple but powerful. It’s easy to forget that encapsulation is more than data hiding. Encapsulation is one of the major concepts of object oriented programming and complete understanding is necessary for success. I’m sure I’ll get interview questions regarding this topic in the future.

The original article: https://javapapers.com/core-java/java-encapsulation/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s