<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">This tutorial describe concept of Encapsulation. It is one of OOPs concept.</p>
<p> Encapsulation is a way of wrapping up data and methods into a single unit. Encapsulation puts the data safe from the outside world by binding the data and codes into single unit. This is best way to protect the data from outside the world.</p>
<p> Encapsulation is nothing but protecting anything which is prone to change. rational behind encapsulation is that if any functionality which is well encapsulated in code i.e maintained in just one place and not scattered around code is easy to change. this can be better explained with a simple example of encapsulation in Java. </p>
<p> <b>Advantage of Encapsulation in Java and OOPS</b></p>
<div class="para"></div>
<div class="para">Here are few advantages of using Encapsulation while writing code in Java or any Object oriented programming language: </div>
<p></p>
<div class="para"></div>
<div class="para">1. Encapsulated Code is more flexible and easy to change with new requirements. </div>
<p></p>
<div class="para"> 2. Encapsulation in Java makes unit testing easy. </div>
<p></p>
<div class="para"> 3. Encapsulation in Java allows you to control who can access what. </div>
<p></p>
<div class="para"> 4. Encapsulation also helps to write immutable class in Java which are a good choice in multi-threading environment. </div>
<p></p>
<div class="para"> 5. Encapsulation reduce coupling of modules and increase cohesion inside a module because all piece of one thing are encapsulated in one place. </div>
<p></p>
<div class="para"> 6. Encapsulation allows you to change one part of code without affecting other part of code. </div>
<div align='center' id="ads-id"></div>
<p><b>Example of Encapsulation ; :</b></div>
<p>
<b>Example 1:</b></p>
<pre class="highlight" name="code">public class Person{ 
private String name; 
private int age; 
private double height; 
private double weight; 
 
public void setName(String name){ 
this.name=name; 
} 
 
public String getName(){ 
return name; 
} 
public void setAge(int age){ 
this.age=age; 
} 
public int getAge(){ 
return age; 
} 
public void setHeight(double height){ 
this.height=height; 
} 
public double getHeight(){ 
return name; 
} 
public void setWeight(double weight){ 
this.weight=weight; 
} 
public double getWeight(){ 
return weight; 
} 
</pre>
<p>
As you can see in the above class all of its properties are private-meaning that they cannot be accessed from outside the class- and the only way to interact with class properties is through its public methods. </p>
<p> <b>Example 2:</b><br />
I have create a class Car which has two methods:</p>
<ol>
<li><b>move().</b></li>
<li><b>internalCombustion()</b></li>
</ol>
<p> The move method depends on <b>internalCombustion()</b> method. But an Employee needs only move method and need not be aware of <b> internalCombustion </b>behavior. So, the <b>internalCombustion </b>behavior can be declared <i><b>private </b></i>so that it can be only visible to the members inside the class.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="429" src="https://dineshonjava.com/wp-content/uploads/2013/04/Encapsulation_in_oops.jpg" width="600"/></div>
</div>
<p><b>Summary:</b></p>
<p> Encapsulation is a primary fundamental principle in Object oriented programming. It is used to enforce the security to restrict the visibility of the members to other components. </p></div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/abstraction-in-java/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/interfaces-in-java/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/abstraction-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/interfaces-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '512'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…