What is encapsulation in c
Encapsulation, in the context of C , refers to an object's ability to hide data and behavior that are not necessary to its user. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.
The following are the benefits of encapsulation:. Encapsulation is used to restrict access to the members of a class so as to prevent the user of a given class from manipulating objects in ways that are not intended by the designer. While encapsulation hides the internal implementation of the functionalities of class without affecting the overall functioning of the system, it allows the class to service a request for functionality and add or modify its internal structure data or methods to suit changing requirements.
Encapsulation is also known as information hiding. Encapsulation in C is implemented with different levels of access to object data that can be specified using the following access modifiers:. Encapsulation can be illustrated with an example of an Employee object that stores details of that object. It is easy to see a situation in which all users could access basic information about an employee while restricting salary information. Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. The variables length, breadth, and height are private. This means that they can be accessed only by other members of the Box class, and not by any other part of your program. This is one way encapsulation is achieved.
The Private data are manipulated indirectly by two ways. Let us see some example programs in C to demonstrate Encapsulation by those two methods. The first method is using a pair of conventional accessor and mutator methods. Another one method is using a named property. Whatever be the method our aim is to use the data without any damage or change.
Let us see an example of Department class. To manipulate the data in that class String departname we define an accessor get method and mutator set method.
Like the above way we can protect the private data from the outside world. Here we use two separate methods to assign and get the required data.
In the above example we can't access the private data departname from an object instance. We manipulate the data only using those two methods. Properties are a new language feature introduced with C. Only a few languages support this property. This concept of encapsulation is used in C language for data hiding and protection.
It can be implemented when the main calling program has an object, the object should be able to find the functions applicable and in the same way, they find the data. We need encapsulation in C to manipulate the access modifiers in C.
Public, private, but they are not explicitly present in C. Although we can make use of this property in C by implementing encapsulation. Encapsulation also provides secure code which can be better understood by an example provided in the below section. This promotes adaptability with changing requirements as whichever code requires a change can be modified in a separate file without changes anything in the main program. This will give simple and error-free code up to some extent.
Data in C language is public by default. This can be achieved by having a separate header and source C files. In the below example: There are three files. This file acts as a link between the data scattered over multiple files.
0コメント