Encapsulation

Encapsulation is the process of wrapping or binding all the functions and data together into a single unit. So we can hide the implementation of the code from the user by using access specifiers. We can make private member variables and have public getters to read data from the variable and setters to write data to the variable, so likewise we can control the access to those variables.


Code: public class TestEncapsulation{
public static void main(String args[]){
Circle c1=new Circle(7.0);
System.out.println("Radius of the circle is:"+c1.getRadius());
System.out.println("Perimeter of the circle is:"+c1.getPerimeter());
c1.setRadius(14.0);
System.out.println("Radius of the circle is:"+c1.getRadius());
System.out.println("Perimeter of the circle is:"+c1.getPerimeter());
}
}

class Circle{
private double radius; private double perimeter;
Circle(double radius){
this.radius=radius; setPerimeter();
}
public void setRadius(double radius){
this.radius=radius; setPerimeter();
} private void setPerimeter(){
this.perimeter=2*this.radius*22/7;
} public double getRadius(){
return this.radius;
} public double getPerimeter(){
return this.perimeter;
}
}

Output:
Radius of the circle is:7.0
Perimeter of the circle is:44.0
Radius of the circle is:14.0
Perimeter of the circle is:88.0

Code Explanation:
As in the above code Circle object have two private member variables and private methods which can be accessed only within the class. we have set public getters and setters to give access to the user. user can read and write values in the radius and also user can read the value in perimeter but cannot change the value on it.

public class TestEncapsulation {
public static void main(String args[]){ // main method
Circle c1=new Circle(7.0); // making a new Circle object
System.out.println("Radius of the circle is:"+c1.getRadius()); //calling getRadius method and display the output 
System.out.println("Perimeter of the circle is:"+c1.getPerimeter()); /*calling getPerimeter method and display the output*/
c1.setRadius(14.0);//Changing the radius
System.out.println("Radius of the circle is:"+c1.getRadius());
System.out.println("Perimeter of the circle is:"+c1.getPerimeter());
}
}

class Circle{ //Circle Class
//member variables
private double radius; private double perimeter;
Circle(double radius){ //Constructor-making a circle object
this.radius=radius; setPerimeter(); //setting the perimeter
}
public void setRadius(double radius){ //User can change the radius
this.radius=radius; setPerimeter();
} private void setPerimeter(){ /*this method cant be accessed from outside since it is private. so user doesn't know how setting perimeter is implemented*/
this.perimeter=2*this.radius*22/7;
} public double getRadius(){ //user can get the radius of the circle object
return this.radius;
} public double getPerimeter(){ //user can get the perimeter of the circle object
return this.perimeter;
}
}

 Encapsulation improves flexibility, maintainability, re-usability of the code and also reduce dependencies. It also control the access to the class  from the outside and hide the implementation which help to improve security.

Comments

Popular posts from this blog

Flow Charts

ධර්මය, ආක්‍රමණය හා යුද්ධය

ආගමක් නැති මනුස්සයෙක්