Monday, June 1, 2020

OOPS Concept - Encapsulation

Binding data with method is called Encapsulation. The idea of encapsulation is to hide the implementation details from users.
To achieve encapsulation in java, you need to:
  1. Declare the variables of a class private so that they can not be accessed directly from outside the class
  2. Provide public setter and getter methods to write and read the values of variables
When we make instance variable private, it can only be accessed within the same class. However, if we setup public setter and getter methods to update the private variable then the outside class can access those private data variable via public methods. So,  in encapsulation, the variable of a class will be hidden from other classes, and can be accessed only through the methods of their current class. 
Setter methodGetter method
  • method must be public
  • return type should be void
  • method name must be prefixed with set
  • it should take some argument
  • method must be public
  • return type should not be void
  • method name should be prefixed with get
  • it should not take any argument

Benefits:

Now, let's talk more about the benefits of encapsulation:
  • Other classes will not be able to access data directly because variables are private.
  • You can make the class read-only (by creating only getter methods) or write-only (by creating only setter methods).
  • You can control your data. In setter and getter methods, you can write the logic for special conditions.
  • It improves flexibility and reusability.
  1. User would not be knowing what is going on behind the scene. They would only be knowing that to update a field call set method and to read a field call get method but what these set and get methods are doing is purely hidden from them.
/A Java class which is a fully encapsulated class.
//It has a private data member and getter and setter methods.
package com.javatpoint;
public class Student{
//private data member
private String name;
//getter method for name
public String getName(){
return name;
}
//setter method for name
public void setName(String name){
this.name=name
}
}

//A Java class to test the encapsulated class.
package com.javatpoint;
class Test{
public static void main(String[] args){
//creating instance of the encapsulated class
Student s=new Student();
//setting value in the name member
s.setName("vijay");
//getting value of the name member
System.out.println(s.getName());
}
}

package com.cerotid.encapsulation;
public class Coat {
private double price;
private String customer;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
}
Second Example:      
package com.cerotid.ratanjavavideos.learning;
public class Encapsulation {
       private int eid;
       private String ename;
       public int getEid() {
             return eid;
       }
       public void setEname(String ename) {
             this.ename = ename;
       }
       public void setEid(int eid) {
             this.eid = eid;
       }
       public String getEname() {
             return ename;
       }
       public static void main(String[] args) {
             Encapsulation enc1 = new Encapsulation();
             enc1.setEid(111);
             enc1.setEname("Ram");
             int eid = enc1.getEid();
             System.out.println(eid);
             String ename = enc1.getEname();
             System.out.println(ename);

             Encapsulation enc2 = new Encapsulation();
             enc2.setEid(222);
             enc2.setEname("Hari");
             System.out.println(enc2.getEid());
             System.out.println(enc2.getEname());
       }
}
Output:
111
Ram
222
Hari


Example III: Best Example
package com.cerotid.encapsulation;

public class Student {
    private String sname;
    private int sroll;
    private double gpa;

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSroll() {
        return sroll;
    }

    public void setSroll(int sroll) {
        this.sroll = sroll;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    public static void main(String[] args) {
        Student obj = new Student();

        System.out.println("Name: " + obj.getSname());
        System.out.println("Roll number: " + obj.getSroll());
        System.out.println("GPA: " + obj.getGpa());

        obj.setSname("Ram");
        obj.setSroll(1);
        obj.setGpa(3.6);

        System.out.println("Name: " + obj.getSname());
        System.out.println("Roll number: " + obj.getSroll());
        System.out.println("GPA: " + obj.getGpa());
    }
}

Output:
Name: null
Roll number: 0
GPA: 0.0
Name: Ram
Roll number: 1
GPA: 3.6

No comments:

Post a Comment