Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Sunday, November 19, 2017

Encapsulation in Java with Examples

What is Encapsulation in Java 

Encapsulation is one of the principle of OOPs.

Encapsulation describes the ability of hiding data and methods of object.
Encapsulation is a process of arranging information about data and behavior into a single component like as object in Java.

Keeping all members and methods are within bounds of class. In Java, all class are under encapsulated.

If we set all fields of class as private, so no other code outside of this class can not access private variables. To access these private variables, we have to declare public methods in class.


So these private members are not accessible by outside objects or classes.

A class can have authority to modify the values using setter and getter methods of class. By using getter methods, we can make read-only and wise versa for write-only.

Encapsulation Example:

package com.java.w3schools.core.abstracts;

public class Employee {
 private String eName;
 private String location;
 public String geteName() {
  return eName;
 }
 public void seteName(String eName) {
  this.eName = eName;
 }
 public String getLocation() {
  return location;
 }
 public void setLocation(String location) {
  this.location = location;
 }
}

public class EmployeMain {
 public static void main(String[] args) {
  Employee employee = new Employee();
  employee.seteName("Jhon");
  employee.setLocation("Chicago");
  System.out.println(employee.geteName());
 }
}

Output :


Jhon

If we delete setter methods from Employee class than object will be read-only.

Encapsulation Real-time Scenario:

In any real time application, if we create a new class then the new class must be used by other programmers in the application.


See the below code developed by programmer 1.

package blog.java.w3schools.encapsulation.Home;

class Home 
{
 public int area; // in sq. feet.
 public String location;


Here, created a Home class with public instance variables first id int area and second is String location.

The below program is developed by programmer 2 and he will use Home class implemented by programmer 1.

package blog.java.w3schools.encapsulation.other.class;

public class OtherClassInApplication {

 public static void main(String[] args) {
  
  Home home1 = new Home();
  home1.area = 1500;
  home1.location = "w3schools";
 }

}

Please refer the above example program. Our application has another class OtherClassInApplication which creates an object for Home class. After creating an object, setting values for the area and location by directly accessing the variables.

After few days, programmer 1 realised that he has used wrong variable name. He changed now to areaInSqft as below.

public int areaInSqft; // Area in sq. feet.


But, this already used by programmer 2 and he will get now compile time error.
 Home home1 = new Home();
 home1.area = 1500; // Compile time error. 
 home1.location = "w3schools";

Compile time error saying "area cannot be resolved or is not a field".

If application is implemented like this then we may have to end up with many errors and difficult to maintain.

Encapsulation Rules:

To take the advantage of Encapsulation and to easy maintainability, flexibility, and extensibility of the code, your design must include the encapsulation principles.

1) Make instance variables as private.
2) Provide public accessor methods, and force calling code to use those methods rather than directly accessing the instance variable. These so-called accessor methods allow users of your class to set a variable's value or get a variable's value.
3) For these accessor methods, use the most common naming convention of set<someProperty> and get<someProperty>.


package blog.java.w3schools.encapsulation;

public class EncapsulationEx {

 public static void main(String[] args) {

  Home home1 = new Home();
  home1.setAreaInSqft(1500);
  home1.setLocation("w3schools");
 }

}

class Home {
 private int areaInSqft; // Area in sq. feet.
 private String location;

 public int getAreaInSqft() {
  return areaInSqft;
 }

 public void setAreaInSqft(int areaInSqft) {
  this.areaInSqft = areaInSqft;
 }

 public String getLocation() {
  return location;
 }

 public void setLocation(String location) {
  this.location = location;
 }
}

Summary:

Java supports Encapsulation principle but it default does not come with Encapsulation. When we write code, we have to implement it using its principles. Encapsulation provides maintainability, flexibility, and extensibility of the code.

If you like the post, Please share the post with your friends or leave your opinion in comment section.

2 comments:

  1. this is very nice and clean example of encapsulation in java.
    this post is well explained in detail.
    thank you for this beautiful post.

    ReplyDelete
  2. Great post. Wonderful information and really very much useful. Thanks for sharing and keep updating.
    Are you looking for the best Java training institute in Chennai?
    Come and learn with Aorta, the best java training institute in Chennai offering the best platform to learn and get the depth of Java
    java training in chennai
    java course in chennai

    ReplyDelete

Please do not add any spam links in the comments section.