blog

Home / DeveloperSection / Blogs / Objective-C : Data Encapsulation

Objective-C : Data Encapsulation

Anonymous User4023 16-Jul-2015

Previously we learn how to manage memory on object creation : Objective-C : Memory management methods

 

All Objective-C programs are composed of the following two fundamental elements:


·Program statements (code): This is the part of a program that performs actions and they are called methods.

·Program data: The data is the information of the program which is affected by the program functions.

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.


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.

 

Encapsulation is an important concept in object-oriented programming. By using class methods to access data stored in object properties, we can separate how that data is returned from how it is stored internally.

 

This is important because the structure of the data and how it is formatted can and will change over time. By encapsulating the data using class methods, we protect ourselves from change by making sure the data returned is always consistent. If we access the object properties directly, we would need to go through our code and make changes every time the structure or format of the data changes.

 

In Objective-C, we use properties, and the accessor methods in particular, help to hide the instance variables themselves. By indirectly accessing them, you get to control their access. You can also force the issue by making them readonly, which means only the containing class can use them. By using methods, you also don’t have to worry about how the instance variables themselves are set up; you simply access the methods that are provided, and have confidence the data returned will always be returned the same way, regardless how it is stored or structured.

 

Bank Account Example


The code for the example Bank application, after being adapted to use properties and synthesized accessors, now reads as follows:


BankAccount.h:


#import <Foundation/Foundation.h>

@interface BankAccount : NSObject

@property double accountBalance; @property long accountNumber;

-(void) setAccount: (long) y andBalance: (double) x;

-(void) displayAccountInfo; @end

 

BankAccount.m:


#import "BankAccount.h" @implementation BankAccount -(void) setAccount: (long) y andBalance: (double) x;

{

        _accountBalance = x;

        _accountNumber = y;

}

-(void) displayAccountInfo

{

        NSLog (@"Account Number %ld has a balance of %f", _accountNumber, _accountBalance);

}

@end


main.m:


#import <Foundation/Foundation.h>

#import "BankAccount.h"

int main(int argc, const char * argv[])

{

    @autoreleasepool {

      BankAccount *account1;

       account1 = [BankAccount alloc];

       account1 = [account1 init];

       [account1 setAccountBalance: 1500.53];

       [account1 setAccountNumber: 34543212];

       [account1 displayAccountInfo];

      [account1 setAccount: 4543455 andBalance: 3010.10];

      NSLog(@"Number = %ld, Balance = %f", [account1 accountNumber], [account1 accountBalance]);

    }

    return 0;

}


The true power of properties is most noticeable in the BankAccount.m implementation file where the use of properties has reduced the number of methods that needed to be implemented from six down to two.

 

 

Next, we will learn about : Objective-C : Categories


Updated 13-Mar-2018
I am a content writter !

Leave Comment

Comments

Liked By