Encapsulation

From Logic Wiki
Jump to: navigation, search


Closing some part of the classes to object users. By using private access modifier some variables can be closed to outer world.

property usage is an example of encapsulation

public class Computer
{ 
  private ushort ram;
  public ushort Ram
  {
     get {return ram;}
     set {ram=value;}
  }
}

Here in this example ram is encapsulated and access is restricted.

Advantages

  • This allows the private data of an object to be hidden from the rest of the program
  • Keep code clean
  • Easy to understand
  • allows classes to be reused in other programs