Wednesday, June 16, 2021

Java OOPs: Encapsulation

Chapter

Java Encapsulation

Encapsulation is the process of wrapping data into a single unit. We can think of encapsulation as a multivitamins capsule. Multivitamins has lots of various vitamins packed in a single capsule.

By using encapsulation, we can achieve data hiding, as the name implies, data hiding is the process of hiding certain data. It can also help us manage and control data for security and flexibility.

Most of the time, we use setters and getters to handle data that comes in and out. Getters and setters are methods that set or get data. For convenience, we name those methods with a prefix "get" for getters and "set" for setters. This example demonstrates encapsulation.

//This class encapsulates main method
public class SampleClass{
   
  
  public static void main(String[]args){
  
   PersonnalInfo myInfo = new PersonnalInfo("B.Ghosts",
                              "123456","246810","Programming");
   System.out.println();
   
   System.out.println("Name: " + myInfo.getName());
   System.out.println("Getting Public Key...");
   System.out.println("Public Key: " + myInfo.getPublicKey());
   System.out.println("Getting Hobby...");
   System.out.println("Hobby: " + myInfo.getHobby());
   System.out.println();
   
   System.out.println("Setting new public key...");
   myInfo.setPublicKey("246810","654321");
   System.out.println("Getting Public Key...");
   System.out.println("Public Key: " + myInfo.getPublicKey());
   System.out.println("Setting hobby...");
   myInfo.setHobby("Playing Games");
   System.out.println("Hobby is set!");
   System.out.println("Getting Hobby...");
   System.out.println("Hobby: " + myInfo.getHobby());
   
  }
}

//This class encapsulates fields(variables),
//methods, etc.
final class PersonnalInfo{
  //If we don't want anyone to
  //directly access class fields
  //then make them private.
  private String name;
  private String publicKey;
  private String privateKey;
  private String hobby;
  
   PersonnalInfo(String name,String publicKey,String privateKey,
                 String hobby){
    
    System.out.println("Creating personnal information of "
                       + name);
    this.name = name;
    System.out.println("Locking public key...");
    this.publicKey = lockKey(publicKey);
    System.out.println("Locking private key...");
    this.privateKey = lockKey(privateKey);
    this.hobby = hobby;
    System.out.println(name + " personnal information has been"+
                       +" created!");
  }
  
  //getters
  public String getName(){ return name; }
  public String getPublicKey(){ return unlockKey(publicKey); }
  public String getHobby(){ return hobby; }
  
  //setters
  public void setPublicKey(String privateKey,String newKey){
   
    if(unlockKey(privateKey).equals(this.privateKey)){
      System.out.println("Private key is matched with"
                        +" the current private key!");
      System.out.println("Changing current public key with"
                         +" the new one...");
      publicKey = lockKey(newKey);
      System.out.println("Public key has been changed"+
                         " successfully!");
    }
    else
      System.out.println("Private key doesn't match the"
                         +" current private key.");
    
  }
  public void setHobby(String hobby){
    this.hobby = hobby;
  }
  
  
  //extra methods for securty
  private String lockKey(String key){
     System.out.println("Verifying Key...");
     System.out.println("Key Signature is valid!");
     System.out.println("Locking Key...");
     System.out.println("Key Locked!");
     return key;
  }
  
  private String unlockKey(String key){
    System.out.println("Unlocking Current Key...");
    System.out.println("Key Unlocked!");
    return key;
  }
}
In the above example, we can set or get data in the class by using getters and setters method. We can see that the variables are more secure because users can't just change the variable values directly. They are required to use the methods that the class provided in order to manipulate data in the class.

No comments:

Post a Comment