This article was originally written as part of a discussion with peers at Walden University. It introduces the concepts of abstraction and encapsulation, subsequently offering illustrations as to how the concepts are used in software development.
Abstraction is “the process of taking away or removing characteristics from something in order to reduce it to set of essential characteristics” (TechTarget, Abstraction, 2019). In real life, people use abstraction all of the time. In my own case, this can be seen clearly in work relationships in my tutoring role at the local community college. Students usually text or call to schedule a meeting, and at the appointed time I will meet the students at the computer center or another large public area on campus. If they have not met me, I abstract my presence for them saying,
“I am tall (6’5”), have dark brown hair, and will be wearing a brown leather jacket.”
I don’t tell them that wear glasses, have brown eyes, wear jeans, or any other personal characteristic. In this case, abstraction allows the object (me) to be properly identified without requiring excessive detail. In this sense, Sandeep Kumar has described abstraction to be the “process of picking the essence of an object” (2015).
In Java class terms, I have described a Martin class, within which is defined all my personal attributes, and contains a constructor which can be used with only height, hair color, and coat style.
Encapsulation is “the inclusion of one thing so that the included thing is not apparent” (TechTarget, Encapsulation, 2019). With encapsulation, the ommission is not necessarily because the information is not needed. In many cases, it may simply be known or assumed. As an example, my neighbor recently used encapsulation in a discussion about home security. He suggested that
“If anyone breaks into my house, my dog will let me know”
Well, if such occasion does occur, my neighbor will be roused by a dog who has been on the watch for unwelcome strangers and is enthusiastically barking. When he described his dog to me, it was known or presumed that the dog would (a) watch out for strangers, and (b) bark loudly if confronted in the home by an unwelcome stranger. It is sufficient to say “My dog will protect my home,” as opposed to saying “My dog will watch out for strangers, bark loudly if he sees one, and wake me up.” The first statement has much about the dog itself encapsulated.
In Java class terms, my neighbor has used the Dog class, with his dog as an object with watchfulness and barking as behaviors encapsulated within that class.
Caution. Both abstraction or encapsulation can lead to less than optimal results. Consider, for example, the case where I have used abstraction to identify my presence based on height, hair color, and jacket style. If I am walking into the computer center to meet the student at the same time as members of the college basketball team, the information I provided would not be enough to identity me completely. The student could suffer a case of mistaken identity.
Kumar, S. (2015, December 1). Encapsulation and Abstraction: Real World Examples. Retrieved from StackOverflow.Com: https://stackoverflow.com/questions/12072980/encapsulation-vs-abstraction-real-world-example
TechTarget. (2019, January 22). Abstraction. Retrieved from TechTarget: WhatIs.com: https://whatis.techtarget.com/definition/abstraction
TechTarget. (2019, January 22). Encapsulation. Retrieved from TechTarget: WhatIs.Com: https://searchnetworking.techtarget.com/definition/encapsulation