Encapsulation in Java with Example

Encapsulation is a basic principle of Java. Let me explain you encapsulation with a simple example.After reading the concept completely, I’m pretty sure you will never need to look back again to learn encapsulation concept.

Encapsulation ensures to hide the implementation details of a class to the other classes. Basic elements in a class are instance variables and member functions. Let us assume that we have written a class “Car”. The class “car” has some public variables like minSpeed, maxSpeed and noOfTyres instances variables some methods using them to calculate some logic.
Now say your friend has written some class called BenzCar and instantiated your Car class and have modified the instance variables of your Car class by mistake by initializing maxSpeed to 0 KMS and minSpeed to 100 KMS which are invalid by logic. So, now you are in serious trouble. Isn’t it?
So, to prevent this kind of blunders you must prevent your Car Class’s instance variables initialization restricted by making them private and adding new public methods which should be the only way to initialize the instance variables of your class.

Simple Example for understanding Encapsulation

Without Encapsulation example:


Class Car
{
Public int minSpeed;
Public int maxSpeed;
Public int noOfTyres;
Public int OptimimSpeed(maxSpeed)
{
Return (maxSpeed)/2;
}
}
package com.developprojects.withOutEncapsulation;

public class BenzCar {
	public static void main(String args[]) {
		Car car = new Car();
		car.minSpeed = 100;
		car.maxSpeed = 0;
		int optimumSpeed = car.optimimSpeed(car.maxSpeed);
		System.out
				.println("OptimimSpeed of Benz Car with out encapsulation ::kms "
						+ optimumSpeed);
	}
}

How to implement Encapsulation in Java:

Encapsulation is essential key feature of Object Oriented Programming. We should follow some simple rules to implement encapsulation in java. They are:

  1. Instance variable needs to safe guarded from other classes by making them private or protected access modifiers
  2. Make methods public and instance variables can only be accessed directly using these public methods. The naming conventions of these methods should be like set<VariableName> and get<VariableName>

With Encapsulation Example:

package com.developprojects.encapsulation;

public class Car {

	private int minSpeed;
	private int maxSpeed;
	private int noOfTyres;

	/**
	 * @return the minSpeed
	 */
	public int getMinSpeed() {
		return minSpeed;
	}

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

	/**
	 * @return the maxSpeed
	 */
	public int getMaxSpeed() {
		return maxSpeed;
	}

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

	/**
	 * @return the noOfTyres
	 */
	public int getNoOfTyres() {
		return noOfTyres;
	}

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

	/**
	 * @return the OptimimSpeed of the vehicle
	 */
	public int optimimSpeed(int maxSpeed) {
		return (maxSpeed) / 2;
	}

}
package com.developprojects.encapsulation;

public class BenzCar {

	public static void main(String args[]) {
		Car car = new Car();
		car.setMaxSpeed(100);
		car.setMinSpeed(0);
		int optimumSpeed = car.optimimSpeed(car.getMaxSpeed());
		System.out.println("OptimimSpeed of Benz Car:: kms " + optimumSpeed);
	}
}

From the above example, we have implemented encapsulation protecting the instance variables. Now even if we change the methods in actual class (Car class here). There is no change in the implementation class (BenzCar class).