![]() |
Encapsulation is an Object-Oriented Programming concept that binds together the data and functions that manipulate the data and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding. Encapsulation in Objective-C is vital for protecting data integrity, and designing robust and maintainable code. Data encapsulation is a fundamental concept in Object-Oriented Programming (OOP) that combines a cohesive set of properties, functions, and other members into a unified entity. Objective-C supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. Syntax and Related keywordsThe syntax for encapsulation involves declaring instance variables within the @interface section of a class. Instance Variable DeclarationIn Objective-C, instance variables are typically declared within the @interface section of a class. Access control keywords (@public, @private, @protected) are used to specify the visibility of these variables.
Here’s the syntax showing the use of access control keywords to specify the visibility of these variables. Syntax:
Explanation: In this example, we declare a Car class with a private instance variable _carModel and a public instance variable _fuelLevel. The use of @private and @public access control keywords restricts the visibility of these variables. Code: ObjectiveC
Output: ![]() Output PropertiesObjective-C introduced properties to simplify the declaration and usage of accessor methods for instance variables. Properties automatically generate getter and setter methods for variables and allow additional configuration. Properties are declared with the @property keyword whereas, Getter and setter methods are generated by @synthesize. Syntax:
Explanation: In the above example, @property declares properties for carModel and fuelLevel. The keywords are nonatomic and strong provide additional property attributes related to memory management and thread safety. Synthesizing AccessorsIn Objective-C, Properties can be synthesized using the @synthesize keyword to automatically generate getter and setter methods for instance variables. This is particularly useful when you have a property in your class, and you want the compiler to handle the creation of the accessor methods. Syntax:
Explanation: In this example, @synthesize automatically creates the getter (carModel) and setter (setCarModel:) methods for the carModel property. The synthesized getter returns the value of the private instance variable _carModel, and the synthesized setter sets the value of _carModel to the provided parameter. Using @synthesize simplifies the implementation and reduces boilerplate code, especially when dealing with multiple properties. Custom Accessor MethodsDevelopers can create custom accessor methods to control the behavior when getting or setting a property’s value. We can define our methods to get or set the property’s value, allowing you to implement custom logic. Syntax:
Explanation: In this example, custom methods setModel: and getModel are used to set and get the value of the _carModel instance variable, providing controlled access to the private data. By using custom accessor methods, you have the flexibility to extend the behavior of property access and modification according to the specific needs of your class. Code: Here’s an example demonstrating data encapsulation in Objective-C by implementing a class with public and private members variables. ObjectiveC
Output: ![]() Output Explanation:
In this code, public addNum and getTotal are the interfaces for the world. The private member total is hidden, but needed for proper functioning of the class. |
Reffered: https://www.geeksforgeeks.org
Objective C |
Related |
---|
![]() |
![]() |
![]() |
|
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |