Encapsulation in java example

In java, by use of the encapsulation in java, we can hide the inner implementation of the class. The inner implementation is only visible in own class but its hide from outside the class. In this post we will see how to use the Encapsulation in java example or encapsulation in oops with example or encapsulation example program in java.

Its software development technique used in OOPs to hide internal object details. In other words, we can say it hides the data members of the class, that maintain the state of the object. Data hiding protects the class members to access from the outside the class.

Encapsulation in java example

By use data hiding a program can be divided into objects with specific data and functions. We can create separate objects with unique data sets and functions, avoiding unnecessary penetration from other program classes.

Let’s discuss encapsulation in java with example, how data hiding protects the details of class members from outside. Suppose you have a class that contains several interdependent fields(variables). So, you should take care that must be in a consistent state. If any programmer is manipulating those fields directly, it may break the consistency of those fields. The programmer may change only one field without changing important related fields.
But instead of this, we can make a method that will communicate to interdependent fields. If the programmer wants to change the field, he just needs to call a method and the method will update all fields.

Let’s discuss it again encapsulation in oops with example, suppose you have a project in which you are dealing with sensitive data. You don’t want to provide the read access of that and that should be right once after that programmer will not able to edit it. You must see the username or email address; it is chosen by only one time after that you can’t change it.
In such type of scenario, we should make the private variable and provides the setter and getter accordingly.

class Account
{
    // For encapsulation all variables should be private
    private String userName;
    private String password;
    private String name;
    private int age;
    private String country;
    
	public Account(String userName, String password, String name, int age, String country) 
	{
		this.userName= userName;
		this.password = password;
		this.name = name;
		this.age = age;
		this.country = country;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
    
}
public class EncapsulationExample
{
    public static void main(String args[])
    {
         Account obj = new Account("javagoal", "myblog", "Ram", 27, "India");
        
         // Here we can update variable those have setter
         
         System.out.println("Name : "+ obj.getName());
         System.out.println("Age : "+ obj.getAge());
         System.out.println("Country : "+ obj.getCountry());
        
    } 
}

Output: Name : Ram
Age : 27
Country : India

1 thought on “Encapsulation in java example”

Leave a Comment