An easy way to understand encapsulation and abstraction

Learning OOP concepts that are especially interested in a deep understanding of abstraction and encapsulation.

Above below

VS abstraction. Information hiding VS encapsulation.

difference between abstraction and encapsulation?

It was very difficult for me to understand these concepts without a real and simple example of a piece of code / code.

One of my colleagues said that abstraction is nothing but the creation of an abstract class and a normal class that protects a member variable called encapsulation.

Is there an easy way to understand and help others understand what exactly they represent and not repeat below?

Abstraction and encapsulation are complementary concepts: abstraction focuses on the observed behavior of the object ... encapsulation focuses on the implementation that generates this behavior ... encapsulation is most often achieved by hiding information that is the process of hiding all the secrets of the object that do not contribute to it essential characteristics.

+66
language-agnostic oop encapsulation abstraction
Apr 15 '13 at 11:42 on
source share
16 answers

Abstraction is a process in which you show only the "relevant" data and "hide" unnecessary details of the object from the user. View your mobile phone, you just need to know which buttons you need to press to send a message or make a call. What happens when you press the button, how your messages are sent, how your calls are connected, they are all selected from the user.

Encapsulation is the process of combining data and functions into a single unit called a class. In encapsulation, data is not directly accessible; it is accessible through functions present inside the class. In simpler words, class attributes are stored in private and public getter and setter methods for managing these attributes. Thus, encapsulation makes the concept of data hiding possible.

enter image description here

+158
Apr 12 '14 at 15:07
source share

Abstraction hides information or provides the client with only the necessary data.

For example, car brakes - you just know that pressing the pedals will stop the car, but you do not need to know how it works inside.

Advantage of abstraction Tomorrow, if the execution of the brake changes from a drum brake to a disc brake, as a client, you do not need to change (i.e. your code will not change)

Encapsulation links data and behavior together in one block. It is also a language mechanism for restricting access to certain components (this can be achieved using access modifiers such as private, protected, etc.).

For example, a Class has attributes (e.g., data) and behavior (e.g., methods that work with this data)

+25
Apr 15 '13 at 11:49
source share

C # usage example

//abstraction - exposing only the relevant behavior public interface IMakeFire { void LightFire(); } //encapsulation - hiding things that the rest of the world doesn't need to see public class Caveman: IMakeFire { //exposed information public string Name {get;set;} // exposed but unchangeable information public byte Age {get; private set;} //internal ie hidden object detail. This can be changed freely, the outside world // doesn't know about it private bool CanMakeFire() { return Age >7; } //implementation of a relevant feature public void LightFire() { if (!CanMakeFire()) { throw new UnableToLightFireException("Too young"); } GatherWood(); GetFireStone(); //light the fire } private GatherWood() {}; private GetFireStone(); } public class PersonWithMatch:IMakeFire { //implementation } 

Any caveman can make fire, because he implements the function IMakeFire . Having a group of firefighters (List) means that both Caveman and PersonWithMatch are valid choices.

It means that

  //this method (and class) isn't coupled to a Caveman or a PersonWithMatch // it can work with ANY object implementing IMakeFire public void FireStarter(IMakeFire starter) { starter.LightFire(); } 

Thus, you can have many developers with a lot of details (properties) and behavior (methods), but in this case their ability to create fire is important. This is an abstraction .

Since some steps are required to create a fire (GetWood, etc.), they are hidden from view because they are an internal class problem. The caveman has many other social behaviors that can be called the outside world. But some details will always be hidden, because they are associated with internal work. They are private and exist only for the object, they are never exposed. This is encapsulation

+13
Apr 15 '13 at 13:16
source share

Abstraction is a generalized term. that is, encapsulation is a subset of abstraction.

Abstraction is a powerful methodology for managing complex systems. Abstraction is governed by clearly defined objects and their hierarchical classification.

For example, a car itself is a well-defined object, which consists of several other smaller objects, such as a transmission system, steering gear, engine, which again have their own subsystems. But for a person, a car is one single object that can be controlled using its subsystems, even if their internal details are unknown. Provided by




Encapsulation: Combining data and method elements together into a single block (such as a class) is called Encapsulation.

Encapsulation is like a shell in a capsule. This includes related operations and data associated with the object in this object.

Encapsulation is similar to your bag in which you can store your pen, book, etc. This means that this is a property of encapsulating members and functions.

 class Bag{ book; pen; ReadBook(); } 

Encapsulation means hiding the internal details of an object, that is, how the object does something.

Encapsulation does not allow clients to see the internal view where abstraction behavior is implemented.

Encapsulation is a method used to protect information in an object from another object.

Hide data for security, for example, make variables private, and open a property to access private data that will be public.

So, when accessing the resource, you can check the data and install it. Provided by

+6
Jul 16 '15 at 10:11
source share

Abstraction is a process in which you "throw out" unnecessary data from an object that you plan to display / present in your project, and save only the properties of the object that are related to your domain.
Example: imagine a car that you would save, for example. model and price, current location and current speed and ignore color and number of places, etc.

Encapsulation is the “binding” of properties and operations that manipulate them in one unit of abstraction (namely, in a class).
Thus, the car will have an accelarate stop , which controls the location and current speed, etc.

+4
Apr 15 '13 at 11:48
source share

Well, I will explain the abstraction with a real example. Let's say you have an electric plug in your house, and many devices can connect to the same outlet, but the plug will never have an idea which device it is connected to, in other words, the details of the devices are abstracted (hidden) from the plug.

Think, what if we connect the device directly to an electric wire without a plug? Tell me, connect the lamp directly to the wire, then the wire will know which device it is connected to, and when we need to replace the lamp, then we need to remove the wire connection with the lamp, which means that the lamp is tightly connected to the wire. In other words, the bulb and conductor know the details with which it is associated, which means not to disengage.

In an object-oriented world, abstraction works the same way. A class that consumes other function / property functions does not need to know which function / property classes it consumes, and everything should be abstracted using an interface / abstract class.

Let me introduce the same example. Here I have the "ElectricPlug" class that the device is running on. But the "ElectricPlug" class does not know which device it is running. It can be any class that implements the IDevice interface, which means that the RunDevice implementation is abstracted from ElectricPlug. Here is the complete code example,

 class Program { static void Main(string[] args) { ElectricPlug electricPlug = new ElectricPlug(new Bulb()); } } public class ElectricPlug { private readonly IDevice _device; public ElectricPlug(IDevice device) { _device = device; } public void Run() { _device.Rundevice(); } } public interface IDevice { void Rundevice(); } public class Bulb : IDevice { public void Rundevice() { Console.WriteLine("Switched on bulb"); } } 
+4
May 30 '17 at 13:12
source share

Encapsulation is what it looks like, a way to put a box around something to protect its contents. An abstraction extracts the functional properties of something that you can perform operations using only what you extracted without knowing about the inner workings.

When we say that two substances are liquids, we use “liquid” as an abstraction over the properties of the substances we are discussing. This abstraction tells us what we can do with substances obtained from our previous experience with liquids.

Abstraction also has nothing to do with hierarchy. You may have another abstraction, such as “metals,” which extracts the properties of substances differently.

Abstractions forget the details, so if you use a specific abstraction, you should not ask about the properties of the main substance that are not provided by the abstraction. For example, if you take milk and water and mix them together, it is not easy for you to ask how much milk you have.

A functor is an abstraction over something that has some idea of ​​a map, i.e. you can run a function on its internal content, which converts the internal bit to something else. Outside something remains the same.

If this becomes useful, it is that if you have a function that works in lists, and you understand that it depends only on the map interface, you can instead depend on the Functor, and then your function can work with streams, promises, maybes, tuples and everything that shares this abstraction.

Functional languages ​​like Haskell have some really great abstraction capabilities that make extreme code practical.

+3
Jun 02 '17 at 4:04 on
source share

Abstraction is like a computer.

You absolutely do not know what happens to him outside of what you see with the graphical interface (graphical user interface) and external equipment (for example, a screen). All these beautiful colors and the like. You present only data relating to you as a general consumer.

Encapsulation is the actual act of hiding irrelevant details .

You use your computer, but you don’t see what its central processor (central processor) looks like (unless you try to get into it). It is hidden (or encapsulated) for all this chrome and plastic.

In the context of OOP (object-oriented programming) languages, you usually have this setting:

 CLASS { METHOD { *the actual code* } } 

An example of “encapsulation” will have a METHOD that a regular user cannot see (private). "Abstraction" is a regular user using METHOD, which they can (public) to use private.

+2
Mar 16 '18 at 19:14
source share

Abstraction is a means of hiding details to simplify the interface.

So, with the car as an example, all the controls in the car are abstractions. This allows you to control the vehicle without understanding the details of the steering, acceleration or deceleration systems.

A good abstraction is one that widely standardizes an interface for many cases of a similar problem. great abstraction can change the industry.

A modern steering wheel, a brake pedal and a gas pedal are all examples of excellent abstractions. The steering of the car initially looked more like the steering of a bicycle. Both brakes and throttles worked manually. But the abstractions we use today were so powerful that they spanned the entire industry.

-

Encapsulation is a way to hide parts in order to protect them from external manipulation.

Encapsulation is what prevents the driver from controlling the movement of the car - from steering stiffness, suspension and braking to the characteristics of the throttle and transmission. Most cars do not provide interfaces for changing any of these things. Such sealing ensures that the car will work as intended by the manufacturer.

Some cars offer a small number of driving modes, such as luxury, sports and economy, which allow the driver to simultaneously change several of these attributes. Providing driving modes, the manufacturer provides the driver with some control over the experience, not allowing him to choose a combination of attributes that would make the vehicle less pleasant or unsafe. Thus, the manufacturer hides the details to prevent unsafe manipulations. This is encapsulation.

+2
Jan 04 '19 at 8:11
source share

data abstraction: access to data elements and member functions of any class is simply called data abstraction .....

Encapsulation: Binding variables and functions or 1 can say that data members or member functions collectively in a single block are called data encapsulation ....

+1
Jul 03 '13 at 9:08
source share

Encapsulation can be considered as wrapping paper used for data binding and collaboration as a single unit that protects it from all types of external dirt (I mean external functions).

Abstraction includes a lack of detail and the use of a simple interface to manage a complex system.

For example, we can light a lamp by pressing a button without worrying about basic electrical engineering (abstraction).

However, you cannot light the lamp in any other way. (Encapsulation)

+1
Aug 25 '16 at 2:34
source share
 public abstract class Draw { public abstract void drawShape(); // this is abstraction. Implementation detail need not to be known. // so we are providing only necessary detail by giving drawShape(); No implementation. Subclass will give detail. private int type; // this variable cannot be set outside of the class. Because it is private. // Binding private instance variable with public setter/getter method is encapsulation public int getType() { return type; } public void setType(int type) { // this is encapsulation. Protecting any value to be set. if (type >= 0 && type <= 3) { this.type = type; } else { System.out.println("We have four types only. Enter value between 0 to 4"); try { throw new MyInvalidValueSetException(); } catch (MyInvalidValueSetException e) { e.printStackTrace(); } } } } 

Abstraction is associated with methods where implementation details are not known, which is a kind of implementation hide.
Encapsulation is associated with the binding of an instance variable to a method, a type of data hiding.

0
Apr 15 '13 at 11:53 on
source share

Data abstraction : DA just filters a specific element. In a class, we can achieve a pure abstraction, because before creating a class, we can only think about the relevant information about the class.

Encapsulation . This is the mechanism by which we protect our data from the outside.

0
Jan 07 '15 at 13:48
source share

When I was at an elementary level in Java, this question also made me think. I did not understand why declaring instance variables as private and setting the setter and getter methods matters. That's why I wrote a blog for this and explained all the advantages of a simple example. What happens if we follow the principle of encapsulation, and if we do not. Hope this will be useful for beginners: https://medium.com/@hasanli.vusala.73/encapsulation-with-simple-example-in- Java-b80f6df3885d

0
Dec 18 '18 at 7:50
source share

The abstraction shows the necessary information to the user, where unwanted data from the user (product from the user) is hidden as encapsulation.

Encapsulation implements abstraction.

Abstraction is the process by which Encapsulation actually implements it. For example, Adding custom logic → we need to check the user, create a database connection and insert the user. Thus, the user does not know that first you need to call the verification function, create a connection to the database, and then insert the value into the database. It calls only the AddUser function, which internally calls all the logic with in, it is only Encapsulation (grouping functions and hiding methods).

0
Feb 13 '19 at 13:33
source share

Encapsulation: I think it has a lot to do with how you can put things together into one entity rather than hide. If you decide to hide something, you can.

Abstraction: Abstraction has a lot to do with hiding things, and there can be different levels of abstraction. For example, in functional abstraction, we can say that it is important to be able to add elements to the list, but the details of how this is done are not of interest and should be hidden. Using data abstraction, we would say that a list is a place where we can store information, but how this list is actually implemented (for example, as an array or as a series of related places) does not matter and should be is hidden.

Link

0
Sep 19 '19 at 4:48
source share



All Articles