Thursday, 31 May 2018

ENCAPSULATION

HOW YOU CAN HIDING THE INFORMATION BY USING ENCAPSULATION?

WHAT IS THE MAIN PURPOSE BY USING ENCAPSULATION?

     The main purpose of encapsulation method is desiging modular commucation protocols in computer networking.Which is logically separte functions in the network are abstrcted from their underlying structures by  high level objects information will be hide.

In the encapsulation it will be present in physical layer it is responsible for physical transmmision area networking and Internet Protocol(IP) it will also provides global addressiing of different computers;Transmmision Control Protocol(TCP) it is process selection. i.e,The port specifies the service such as aweb or tftp server.

Each encapsulation layer builds a Protocol Data Unit(PDU) we can add aheader (and sometimes trailer) it will contain the control information of PDU from the layer above.for example,in the internet protocol suite, the contents of a webpage are encapsulation with an HTTP header,then by a TCP header, and IP header, and finally, by aframe header and trailer. The frame is forwarded to destination node as a stream of bits,where it is de-encapsulation (or decapsulation) in to the respective PDUS and interpreted at each layer by the reciving node.

Each lower layer provides  aserrvice to the layer or above layer it for the results of encapsulation, at the sometime each layer communicates with its near layer on the reciving node. It will know as adjacent layer interaction and same layer interaction respectively.

It will be present on more abstract layer and more specific layer's is called as upper layer protocol and lower layer protocols.Some times the both layer protocols are used to describe the layers above and below IP respectively.




                                                     

 This effectively hides the information from lower layers of encapsulation


Encapsulation (computer programming):

 encapsulation can be used to refer to one of two related but distinct notions, 
  • A  progrmming language mechanism limiting for  direct access to some of the object's components. 
  • A  construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
A lot of programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of object-oriented programming, while other programming languages which provide lexical closures view encapsulation as a feature of the chinese language orthogonal to object orientation.
The other definition is motivated by the fact that in many OOP languages concealing of components is not programmed or can be overridden; thus, information covering is defined as a separate notion by those who prefer the second definition.
.The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist.
Encapsulation information hiding mechanism:


Below program is example of c# that how access to a data field by using private keyword:


class Program {
 public class Account {
  private decimal accountBalance = 500.00m;

  public decimal CheckBalance() {
   return accountBalance;
  }
 }

 static void Main() {
  Account myAccount = new Account();
  decimal myBalance = myAccount.CheckBalance();

  /* This Main method can check the balance via the public
  * "CheckBalance" method provided by the "Account" class 
  * but it cannot manipulate the value of "accountBalance" */
 }
}

Below is an example in Java:
public class Employee {
    private BigDecimal salary = new BigDecimal(50000.00);
    
    public BigDecimal getSalary() {
        return salary;
    }

    public static void main() {
        Employee e = new Employee();
        BigDecimal sal = e.getSalary();
    }
}
Below is an example in PHP:
class Account {
    /**
     * How much money is currently in the account
     * @var float
     */
    private $accountBalance;

    /**
     * @param float $currentAccountBalance Initialize account to this dollar amount
     */
    public function __construct($currentAccountBalance) {
        $this->accountBalance = $currentAccountBalance;
    }

    /**
     * Add money to account
     * @param float $money Dollars to add to balance
     * @return void
     */
    public function deposit($money) {
        $this->accountBalance += $money;
    }

    /**
     * Remove money from account
     * @param float $money Dollars to subtract from balance
     * @throws Exception
     * @return void
     */
    public function withdraw($money) {
        if ($this->accountBalance < $money) {
            throw new Exception('Cannot withdraw $' . $money . ' from account as it contains $' . $this->accountBalance);
        }
        $this->accountBalance -= $money;
    }

    /**
     * Get current account balance, that takes all additions and subtractions into consideration.
     * @return float
     */
    public function getAccountBalance() {
        return $this->accountBalance;
    }
}

// Create a new object from the Account class with a starting balance of $500.00
$myAccount = new Account(500.00);

// We have clearly defined methods for adding and subtracting money from the Account
// If we didn't have a method for withdraw(), nothing would prevent us from withdrawing more money than was available in the account
$myAccount->deposit(10.24);
$myAccount->withdraw(4.45);

// Get the current balance
$accountBalance = $myAccount->getAccountBalance();
echo 'My Account Balance: $' . $accountBalance; // 505.79

// Our code forbids us from withdrawing more than we have
$myAccount->withdraw(600.00); // Exception Message: Cannot withdraw $600 from account as it contains $505.79
Encapsulation is also possible in non-object-oriented languages. In C, for example, a structure can be declared in the public API (i.e., the header file) for a set of functions that operate on an item of data containing data members that are not accessible to clients of the API:
// Header file "api.h"

struct Entity;          // Opaque structure with hidden members

// API functions that operate on 'Entity' objects
extern struct Entity *  open_entity(int id);
extern int              process_entity(struct Entity *info);
extern void             close_entity(struct Entity *info);
Clients call the API functions to allocate, operate on, and deallocate objects of an opaque data type. The contents of this type are known and accessible only to the implementation of the API functions; clients cannot directly access its contents. The source code for these functions defines the actual contents of the structure:
// Implementation file "api.c"

#include "api.h"

// Complete definition of the 'Entity' object
struct Entity {
    int     ent_id;         // ID number
    char    ent_name[20];   // Name
    ... and other members ...
};

// API function implementations
struct Entity * open_entity(int id)
{ ... }

int process_entity(struct Entity *info)
{ ... }

void close_entity(struct Entity *info)
{ ... }
















No comments: