Encapsulation in Java

To Gather all related methods and attributes in a single class is called Encapsulation. In other words, binding related data members and methods in a class is called Encapsulation. Encapsulation class is also called expert class because it binds only related attributes and methods.

Making an expert class is called encapsulation or you can say after encapsulation we will get an expert class.

Here is an example of expert classes Person, Account and AutoMobile.

Encapsulation in Java, Encapsulation in Java Example

The person can contain information relevant to a person like a name, address, and date of birth but cannot contain irrelevant information like balance or speed.

Account class is the expert of account services and contains relevant attributes and methods like fundTransfer(), deposit(), withdrawal() and getBalance(). It cannot contain a piece of irrelevant information like date of birth and gear.

Automobile class is likewise an expert class and binds members only pertaining to the automobile.

Look at the description of each encapsulate classes

Encapsulation in Java Example

Person Class

package test;

import java.util.Date;

public class Person {

	private String name;
	private String address;
	private Date dateOfBirth;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the dateOfBirth
	 */
	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	/**
	 * @param dateOfBirth the dateOfBirth to set
	 */
	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

}

Account Class

package test;

public class Account {

	private String accountNumber;
	private String accountType;
	private double accountBalance;

	/**
	 * @return the accountNumber
	 */
	public String getAccountNumber() {
		return accountNumber;
	}

	/**
	 * @param accountNumber the accountNumber to set
	 */
	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}

	/**
	 * @return the accountType
	 */
	public String getAccountType() {
		return accountType;
	}

	/**
	 * @param accountType the accountType to set
	 */
	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	/**
	 * @return the accountBalance
	 */
	public double getAccountBalance() {
		return accountBalance;
	}

	/**
	 * @param accountBalance the accountBalance to set
	 */
	public void setAccountBalance(double accountBalance) {
		this.accountBalance = accountBalance;
	}

}

AutoMobile Class

package test;

public class AutoMobile {

	private String color;
	private float speed;
	private int gear;

	/**
	 * @return the color
	 */
	public String getColor() {
		return color;
	}

	/**
	 * @param color the color to set
	 */
	public void setColor(String color) {
		this.color = color;
	}

	/**
	 * @return the speed
	 */
	public float getSpeed() {
		return speed;
	}

	/**
	 * @param speed the speed to set
	 */
	public void setSpeed(float speed) {
		this.speed = speed;
	}

	/**
	 * @return the gear
	 */
	public int getGear() {
		return gear;
	}

	/**
	 * @param gear the gear to set
	 */
	public void setGear(int gear) {
		this.gear = gear;
	}
}

An object is consisting of state (variables and fields) and behavior (methods/functions).

For practical reasons, an object may wish to expose some of its variables or hide some of its methods.

Access modifiers private, protected, and the public are used to expose or hide attributes and methods in an object.

Generally, state (attributes) of an object is made private and can be accessed through its public behavior (methods).

Variables/fields contained by class are called member attributes and functions contained by a class are called member methods.

Member attributes and member methods both are collectively called a member of the class.

Here in Person class name, address and dateOfBirth are member attributes whereas getName(), setName(String name), getAddres(), setAddress(), getDateOfBirth(), setDateOfBirth(Date date) are member methods.

Simple Example of Encapsulation in Java

/* File name : EncapTest.java */
public class EncapTest {
   private String name;
   private String idNum;
   private int age;

   public int getAge() {
      return age;
   }

   public String getName() {
      return name;
   }

   public String getIdNum() {
      return idNum;
   }

   public void setAge( int newAge) {
      age = newAge;
   }

   public void setName(String newName) {
      name = newName;
   }

   public void setIdNum( String newId) {
      idNum = newId;
   }
}

/* File name : RunEncap.java */
public class RunEncap {

   public static void main(String args[]) {
      EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");

      System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
   }
}

Output

Name: James Age: 20

Benefits of Encapsulation:

1. Expose only relevant data to the user.


2. Fields and member methods can be controlled over access. Fields can be made read-only.


3. All related information can be found by using a single class.

Share