{"id":1214,"date":"2022-02-25T16:54:57","date_gmt":"2022-02-25T16:54:57","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1214"},"modified":"2022-02-25T16:54:57","modified_gmt":"2022-02-25T16:54:57","slug":"java-encapsulation-2","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-encapsulation-2\/","title":{"rendered":"Java Encapsulation"},"content":{"rendered":"\n<p>In this tutorial, you will learn about encapsulation and data hiding in Java with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction\">Java Encapsulation<\/h2>\n\n\n\n<p>Encapsulation is one of the key features of object-oriented programming. Encapsulation refers to the bundling of fields and methods inside a single class.<\/p>\n\n\n\n<p>It prevents outer classes from accessing and changing fields and methods of a class. This also helps to achieve&nbsp;<strong>data hiding<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example 1: Java Encapsulation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Area {\n\n  \/\/ fields to calculate area\n  int length;\n  int breadth;\n\n  \/\/ constructor to initialize values\n  Area(int length, int breadth) {\n    this.length = length;\n    this.breadth = breadth;\n  }\n\n  \/\/ method to calculate area\n  public void getArea() {\n    int area = length * breadth;\n    System.out.println(\"Area: \" + area);\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create object of Area\n    \/\/ pass value of length and breadth\n    Area rectangle = new Area(5, 6);\n    rectangle.getArea();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Area: 30<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a class named&nbsp;<var>Area<\/var>. The main purpose of this class is to calculate the area.<\/p>\n\n\n\n<p>To calculate an area, we need two variables:&nbsp;<var>length<\/var>&nbsp;and&nbsp;<var>breadth<\/var>&nbsp;and a method:&nbsp;<code>getArea()<\/code>. Hence, we bundled these fields and methods inside a single class.<\/p>\n\n\n\n<p>Here, the fields and methods can be accessed from other classes as well. Hence, this is not&nbsp;<strong>data hiding<\/strong>.<\/p>\n\n\n\n<p>This is only&nbsp;<strong>encapsulation<\/strong>. We are just keeping similar codes together.<\/p>\n\n\n\n<p><strong>Note<\/strong>: People often consider encapsulation as data hiding, but that&#8217;s not entirely true.<\/p>\n\n\n\n<p>Encapsulation refers to the bundling of related fields and methods together. This can be used to achieve data hiding. Encapsulation in itself is not data hiding.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why\">Why Encapsulation?<\/h2>\n\n\n\n<ul><li>In Java, encapsulation helps us to keep related fields and methods together, which makes our code cleaner and easy to read.<\/li><li>It helps to control the values of our data fields. For example,<code>class Person { private int age; public void setAge(int age) { if (age &gt;= 0) { this.age = age; } } }<\/code><br>Here, we are making the&nbsp;<var>age<\/var>&nbsp;variable&nbsp;<code>private<\/code>&nbsp;and applying logic inside the&nbsp;<code>setAge()<\/code>&nbsp;method. Now,&nbsp;<var>age<\/var>&nbsp;cannot be negative.<\/li><li>The getter and setter methods provide&nbsp;<strong>read-only<\/strong>&nbsp;or&nbsp;<strong>write-only<\/strong>&nbsp;access to our class fields. For example,<code>getName() \/\/ provides read-only access setName() \/\/ provides write-only access<\/code><\/li><li>It helps to decouple components of a system. For example, we can encapsulate code into multiple bundles.<br><br>These decoupled components (bundle) can be developed, tested, and debugged independently and concurrently. And, any changes in a particular component do not have any effect on other components.<\/li><li>We can also achieve data hiding using encapsulation. In the above example, if we change the length and breadth variable into private, then the access to these fields is restricted.<br><br>And, they are kept hidden from outer classes. This is called&nbsp;<strong>data hiding<\/strong>.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-hiding\">Data Hiding<\/h2>\n\n\n\n<p>Data hiding is a way of restricting the access of our data members by hiding the implementation details. Encapsulation also provides a way for data hiding.<\/p>\n\n\n\n<p>We can use\u00a0access modifiers\u00a0to achieve data hiding. For example,<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Data hiding using the private specifier<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n\n  \/\/ private field\n  private int age;\n\n  \/\/ getter method\n  public int getAge() {\n    return age;\n  }\n\n  \/\/ setter method\n  public void setAge(int age) {\n    this.age = age;\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of Person\n    Person p1 = new Person();\n\n    \/\/ change age using setter\n    p1.setAge(24);\n\n    \/\/ access age using getter\n    System.out.println(\"My age is \" + p1.getAge());\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>My age is 24<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have a&nbsp;<code>private<\/code>&nbsp;field&nbsp;<var>age<\/var>. Since it is&nbsp;<code>private<\/code>, it cannot be accessed from outside the class.<\/p>\n\n\n\n<p>In order to access&nbsp;<var>age<\/var>, we have used&nbsp;<code>public<\/code>&nbsp;methods:&nbsp;<code>getAge()<\/code>&nbsp;and&nbsp;<code>setAge()<\/code>. These methods are called getter and setter methods.<\/p>\n\n\n\n<p>Making&nbsp;<var>age<\/var>&nbsp;private allowed us to restrict unauthorized access from outside the class. This is&nbsp;<strong>data hiding<\/strong>.<\/p>\n\n\n\n<p>If we try to access the&nbsp;<var>age<\/var>&nbsp;field from the&nbsp;<var>Main<\/var>&nbsp;class, we will get an error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ error: age has private access in Person\np1.age = 24;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about encapsulation and data hiding in Java with the help of examples. Java Encapsulation Encapsulation is one of the key features of object-oriented programming. Encapsulation refers to the bundling of fields and methods inside a single class. It prevents outer classes from accessing and changing fields and methods of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1214"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1214"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1214\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}