Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

Difference between Encapsulation and Abstraction with Comparison Chart

|| 0

Difference between Encapsulation and Abstraction with Comparison Chart
Stechies

Abstraction and Encapsulation form important concepts related to Object Oriented programming (OOP). Both these concepts of data abstraction and encapsulation are responsible for the integration of real-world objects into codes and programs. Even though these two concepts are likely to go hand-in-hand, the abstraction and encapsulation difference has to be understood clearly so as to draw optimum benefits from them. An important interview question, “What is the difference between abstraction and encapsulate,” draws different answers from the interviewees. Herein, it becomes important for them to refer to the difference between encapsulation and abstraction in c++ and other OOP languages to get the right response for their answers.

This article aims to bring out the difference between abstraction and encapsulation with example wherever possible. Alongside, you will get to know more about the features and definition of encapsulation and abstraction as you go through the various subheadings in this article. Read on for more.

Abstraction vs Encapsulation

Given below is a comparison chart that depicts the difference between data abstraction and data encapsulation in a lucid manner:

BASIS FOR COMPARISON

ABSTRACTION

ENCAPSULATION

Basic difference

Abstraction shows the elements that are important for building a system.

Encapsulation conceals the complexity of a system.

Application

Abstraction method in OOP is applied at the 'design level'.

Encapsulation method in OOP is applied at the 'implementation level'.

Main focus

This method lays emphasis on "what" should be performed

The emphasis is laid on "how" a method should be performed.

How achieved

Achieved via encapsulation.

Achieved once the members of a class are designated as 'private'.

Example

The graphic user interface of a mobile device has certain icons that can be clicked upon. When clicked these icons are known to perform certain functions. Internal circuits, LEDS, etc. are all examples of encapsulation; they combine to form a TV.

Once the icon on the GUI is clicked, end user will have no notion with respect to the implementation details. For example, abstraction refers to the on-off, channel, sound setting as well as the other buttons that are provided for the operation of a TV.

Function

Encapsulation binds data members and the member functions into singular units referred to as a class.

Abstraction is used mainly for hiding the complexity of implementation and providing easy accessibility to users.

Hiding of variables/implementations

Encapsulation hides variables or certain implementations that may be edited quite often in a class with a view of preventing outsiders from accessing the same directly. These details are accessed with the help of getter and setter type methods.

Abstraction comes in use for hiding certain variables/ implementations/ elements but in higher degree such as class, interface, etc. The clients may use an abstract classes/ interfaces with just the information pertaining to what they do rather than  what they are.

Nature of process

It is the process as well as the result of the identification of common essential characteristics pertaining to a set of objects. Abstraction is basically the process of generalization wherein all objects under consideration can be found in a superset of objects; these objects possess certain properties but happen to be quite varied in other respects.

It is the process of encompassing functions and data and manipulating the given data into a singular unit. This is done to hide internal implementations from being seen by the outside world.

Use of class and interface

In abstraction, the details are not available in a class/ interface. It is usually done with a view of providing polymorphic access to any given set of classes. Abstract classes are not capable of being instantiated, therefore another class has to be derived from them for creating more concrete representations.

In encapsulation the data details are well-hidden within a class. It hides the implementation complexity of specific classes from the client desirous of using the same. Here, the "client" may be an event/program or the person responsible for writing the class.

What is Encapsulation?

Encapsulation is an important OOP concept that binds functions and data into singular components even while taking the necessary measures for restricting access to certain components.

  • This fundamental concept of OOP helps users find information under one unit only.
  • Technically, encapsulation refers to the hiding of attributes with a view of shielding variables from external access. This method disallows the changes in one portion of an application from affecting the other parts in any way.
  • There is a risk of misuse of data that takes place with information becoming more open. Encapsulation offers the basic integrity to data by keeping it safe and protected from the outside world. In other words, it ensures that all extra details remain well-hidden from the outside world.
  • Encapsulation serves as an important mechanism in Java for the purpose of wrapping the code and data (variables) that act on data (methods) in unison and in the form of a singular unit.
  • This method hides the variables of any given class from the other classes. The variables may be accessible only via the methods pertaining to their current class. Given this feature of encapsulation, it is also called data binding.
  • In order to achieve encapsulation in Java, the variables of a class have to be declared as private. The private class members are incapable of accessing the code as defined outside the class. The setter and getter methods (public) have to be provided for the sake of modifying and viewing variables values.
  • Encapsulation is mostly about implementation. The main utility of this method lies in its capacity of hiding the internal working and nature of objects from the outside world.
  • Users can make changes or go about their work without impacting outside clients. It is not essential for the client to make changes to the code in order to benefit from the same; the mechanism binds data/code in one place as well as keeps the same safe from all outside interferences.
  • It serves as a protective wrapper for the data/code within the class, from being accessed by the codes that are defined outside the member function/methods/class, etc.

Overall, the method prevents misuse of secured data along with hiding the complexity of codes. For instance, in case of a Bluetooth mouse, as a user, all that you need to know is the behaviour of the device without being concerned about the details of implementation, the kind of sensors possessed by the device, whether it has wireless capabilities or not, etc. Each single detail would give a description of the mouse; notwithstanding the details, the device would remain to be just a mouse. The mouse pointer serves as an interface for working with the device; this method of application and use can be referred to as encapsulation.

What is abstraction?

Abstraction refers to the mechanism of extracting and accessing essential elements that are responsible for creating systems without their implementation details. In this method, developers and users need to lay emphasis on just what needs to done rather than how the same should be done. In other words, abstraction pertains to a concept or thought process that provides solutions at the design level.

  • Hierarchical classification allows for the management of complex abstraction. It helps in the breaking down of complex systems into manageable pieces for the creation of layered semantics.
  • Abstraction is known for handling ideas rather than events. For example, when you send an email, the protocols used by the email server, what happens once the email is sent, etc. are hidden from your eyes. All that you need to do is type the content of the email, add the address of the recipient and click on send. Similarly, in OOP, abstraction hides all implementation details from users. The information pertaining to the functionality of the object, method, class, etc. is the only thing available to users who get to know what happens rather than how something happens.
  • In Java, it is possible to achieve this method with the help of abstract interfaces and classes.
  • Abstraction hides complexity via the delivery of more abstract pictures whereas encapsulation hides all internal working with the aim of allowing users to change the same later on.
  • Abstraction hides the necessary details right at the design level and not at the implementation level.
  • There are various levels of abstraction. Given this, it is recommended that classes should be made to interact with others having a similar or higher level of abstraction. As the level of abstraction becomes higher, things get simpler as the details are left out.

Abstraction basically refers to a “generalization”. For instance, a bike comprises of several subsystems of the likes of engine, sound system, brakes, lights, seat, etc. that are assembled for creating the object bike. Even though the subsystem brake can be further categorized as the front and back brake wheel systems, in general, this specific subsystem is referred to as brakes. Thus, hierarchical classification helps in the management of complex systems and helps users find out the essential elements which are needed for creating any system. Thus, abstraction can be referred to as generalizing “the elements that are essential for the creation of systems”, by opting for the important ones only.

Now that you are conversant with the definition of abstraction and its main features, as well as the definition of encapsulation and its main features, it would help to understand the difference between abstraction and encapsulation point-wise.

Key difference between Encapsulation and Abstraction

The main difference between abstraction and encapsulation can be highlighted in the form of points as below:

1) Abstraction provides solutions to problems at the design level; encapsulation provides solutions at implementation level.

2) Abstraction refers to a fundamental OOP method/ concept that emphasizes on the essential aspects of any object. It functions by hiding all irrelevant details with a view of increasing efficiency and eliminating complexity. Encapsulation, serves as a data hiding mechanism that wraps up data and information within a capsule with an aim of restricting access to the outside world.

3) Abstraction relates to the hiding away of all unwanted details and information while showing the most essential details to users. In encapsulation, the data and codes are hidden within singular units that may be in the form of an interface, class or method. Through encapsulation, the details of the inner functionality of objects remain hidden from the outside world.

4) Abstraction refers to generalizing things or extracting common details. It allows users to lay emphasis on what functionality is performed by a specific object rather than how things work. In contrast, encapsulation refers to the hiding of all internal details of the way in which any object works. These details can be changed at any later stage by replacing the same with better methods, provided they have been kept as private.

5) Abstraction highlights the outer lookout, for instance, the movement of a bike. On the other hand, encapsulation focuses on the internal working or the inner lookout, which effectively means how the bike moves.

6) In java programs, abstraction finds support in abstract classes and interface. In encapsulation, access modifiers- modifiers are used to encapsulate data: private, public, protected internal, internal and protected – are used for achieving the method.

Conclusion

Th above difference between encapsulation and abstraction in OOP languages, including java and c++ will help you apply the concepts in better ways. Though both may seem similar to any layman’s eyes, these concepts are quite dissimilar and behave in different ways. As you get exposed to more complex codes you will get to understand how abstraction and encapsulation work in reality. In case you have any further inputs on the diff between abstraction and encapsulation, or if you have queries related to data abstraction vs data encapsulation, we would like to hear from you. Do write to us in the Comments section below, we shall await your response.


Advertisement