{"id":702,"date":"2016-02-04T09:29:23","date_gmt":"2016-02-04T09:29:23","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=702"},"modified":"2016-02-25T06:25:26","modified_gmt":"2016-02-25T06:25:26","slug":"encapsulation","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/encapsulation","title":{"rendered":"Encapsulation With Example And Program In JAVA"},"content":{"rendered":"<p>Encapsulation is one of the four key concepts in OOPS (Object Oriented Programming) namely <a href=\"\/java\/inheritance\">Inheritance<\/a>, Encapsulation, <a href=\"\/java\/abstraction\">Abstraction<\/a> and Polymorphism. Following definition helps you to understand the concept of encapsulation:<\/p>\n<ul>\n<li>Encapsulation is a process of hiding the data from the users or in other words we can say it protects the code by preventing access from the outside class.<\/li>\n<li>We can say wrapping up of data member and member functions together into a single unit (i.e. Class) is called Encapsulation.<\/li>\n<li>To achieve abstraction in JAVA we set data fields as private which means now no outside class can access it. However to allow outside class to read and write the data on those data fields we create public getter and setter methods.<\/li>\n<li>Encapsulation means hiding the internal details of an object i.e. how an object does something. It prevents clients from seeing its inside view, where the behavior of the abstraction is implemented.<\/li>\n<li>Encapsulation is also known as Data Hiding<\/li>\n<\/ul>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span>\u00a0Encapsulation is achieved by making the fields private and using setter &amp; getter public methods to set and get data in it.<\/p>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Encapsulation Explanation With Example In JAVA:<\/strong><\/span><\/h4>\n<p>Now we will explain the concept of Encapsulation with example.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> In the below example, we will set two fields as private name and email in class UserDetails. So these fields can only be access within this class and no outside class can access it. Also here we will create public methods getter (Ex. getName()) and setter (Ex. setName()) to read and write data on those fields from outside class. By doing this we are hiding the fields and its implementation from outside class. <em>This whole process is known as Encapsulation which is also called as Data Hiding<\/em>.<\/p>\n<p><strong><span style=\"color: #008000;\">Step 1:<\/span><\/strong> Create a class UserDetails, two private fields name and Email. Also create public getter and setter methods.<\/p>\n<pre>\/\/Save as UserDetails.java\r\n\r\npublic class UserDetails\r\n{\r\n\r\n\u00a0\u00a0 \u00a0private String name;\r\n\u00a0\u00a0 \u00a0private String email;\r\n\r\n\u00a0\u00a0 \u00a0public String getName()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return name;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public void setName(String name)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.name = name;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public String getEmail()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return email;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public void setEmail(String email)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.email = email;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Create a new class User. Here create a object for UserDetails in main method. On object call method setName() and setEmail() to write values and then call method getName() and getEmail() to read values.<\/p>\n<pre>\/\/Save as User.java\r\n\r\npublic class User {\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0UserDetails userDetails = new UserDetails();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0userDetails.setName(\"AbiAndroid\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0userDetails.setEmail(\"info@abhiandroid.com\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Name: \"+userDetails.getName());\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Email: \"+userDetails.getEmail());\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Name: AbiAndroid\r\nEmail: info@abhiandroid.com<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> The public setName(), setEmail(), getEmail() and getName() methods are the access points of the instance variables of the UserDetails class. Normally, these methods are referred as <strong>getters and setters<\/strong> in JAVA. Therefore any outside class that wants to access those variables should access them through these getters and setters only.<\/p>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Real Life Encapsulation Example in Java:<\/strong><\/span><\/h4>\n<p>Lets try to understand the concept of Encapsulation with one more example. Here we will use FruitDetails class which has all related data fields like name, price and color.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create class FruitDetails and create three private fields for name, price and color. Also create constructor which will be used for initializing values of FruitDetails object that we will create other class.<\/p>\n<p>After that create Getter and Setter for all fields.<\/p>\n<pre>\/\/Save as FruitDetails.java\r\n\r\npublic class FruitDetails\r\n{\r\n\u00a0\u00a0 \u00a0\/\/Data members of class FruitDetails\r\n\u00a0\u00a0 \u00a0private String name;\r\n\u00a0\u00a0 \u00a0private String price;\r\n\u00a0\u00a0 \u00a0private String color; \r\n\r\n\u00a0\u00a0 \u00a0\/\/declaration of constructor\r\n\u00a0\u00a0 \u00a0public FruitDetails(String name, String price, String color)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.name = name;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.price = price;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.color = color; \r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\/\/Declaring Setter for all fields\r\n\u00a0\u00a0 \u00a0public void setName(String name) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.name = name;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public void setPrice(String price) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.price = price;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public void setColor(String color) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.color = color;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0\/\/Declaring Getter for all fields\r\n\u00a0\u00a0 \u00a0public String getName()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return name;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public String getPrice()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return price;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public String getColor()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return color;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Create another class Fruit and here create object for FruitDetails class. Here fill the details of Fruit suppose apple in constructor. After that use getter to get the value and print it. Then change the initialized value using setter and again use getter to print the updated value.<\/p>\n<pre>\/\/Save as Fruit.java\r\n\r\npublic class Fruit {\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0FruitDetails apple = new FruitDetails(\"Apple\",\"$3\",\"Red\"); \/\/Using constructor to inititialize\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Using getter to the value\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Name: \"+ apple.getName()+\" Price: \"+apple.getPrice()+\" Color: \"+apple.getColor());\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Lets update the price and color using setter\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0apple.setColor(\"Green\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0apple.setPrice(\"$7\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Values of Apple after updation\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Using getter to get the value\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Name: \"+ apple.getName()+\" Price: \"+apple.getPrice()+\" Color: \"+apple.getColor());\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}\r\n\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Name: Apple Price: $3 Color: Red\r\nValues of Apple after updation\r\nName: Apple Price: $7 Color: Green<\/pre>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Importance Of Encapsulation:<\/strong><\/span><\/h4>\n<ul>\n<li>The fields of a class can be made read-only or write-only.<\/li>\n<li>Class can have total control over what is store in its fields.<\/li>\n<li>The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.<\/li>\n<li>The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code.<\/li>\n<li>Encapsulation gives maintainability, flexibility and extensibility to our code.<\/li>\n<li>Encapsulation in Java makes unit testing easy.<\/li>\n<li>Encapsulation allows you to change one part of code without affecting other part of code.<\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Important Points To Remember:<\/strong><\/span><\/h4>\n<ul>\n<li>&#8220;Encapsulation is accomplished by using Class i.e. keeping data and methods into a single unit&#8221;\u00a0.<\/li>\n<li>Encapsulation is a technique used to protect the information in an object from the other object.<\/li>\n<li>Encapsulation is said to be providing \u201caccess control\u201d through which we can control which parts of the program can access the members of any class and thus prevent misuse. Various access controls are public, private and protected.<\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Difference Between Abstraction And Encapsulation :-<\/strong><\/span><\/h4>\n<table border=\"1\">\n<tbody>\n<tr>\n<td width=\"308\"><strong>Abstraction<\/strong><\/td>\n<td width=\"308\"><strong>Encapsulation<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"308\">1. In java, Abstraction solves the problem in the design level.<\/td>\n<td width=\"308\">1. In java, Encapsulation solves the problem of the implementation level.<\/td>\n<\/tr>\n<tr>\n<td width=\"308\">2. Abstraction is used for hiding the unwanted data and gives relevant data.<\/td>\n<td width=\"308\">2. Encapsulation means hiding the code and data into a single unit to protect the data from outside world.<\/td>\n<\/tr>\n<tr>\n<td width=\"308\">3. Abstraction lets you focus on what the object does instead of how it does it.<\/td>\n<td width=\"308\">3. Encapsulation means hiding the internal details or mechanics of how an object does something.<\/td>\n<\/tr>\n<tr>\n<td width=\"308\">4.\u00a0Abstraction is an Outer layout, used in terms of design. For Example: Outer Look of a Mobile Phone, like it has a keypad buttons to dial a number and display screen.<\/td>\n<td width=\"308\">4.\u00a0Encapsulation is an Inner layout, used in terms of implementation. For Example: Inner Implementation detail\u2019s of a Mobile Phone, how\u2019s Display Screen and keypad button are connect with each other using circuits.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Real World Example To Better Understand Abstraction And Encapsulation Difference:<\/strong><\/p>\n<p>You have a Mobile Phone, you can dial a number using keypad buttons. Even you don&#8217;t know how these are working internally. This is achieved in JAVA using Abstraction. But how the Mobile Phone internally working? How keypad buttons are connected with internal circuit? This is achieved using Encapsulation.<\/p>\n<p>You can read <a href=\"\/java\/abstraction\">Abstraction Tutorial<\/a> for more details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Encapsulation is one of the four key concepts in OOPS (Object Oriented Programming) namely Inheritance, Encapsulation, Abstraction and Polymorphism. Following definition helps you to understand the concept of encapsulation: Encapsulation is a process of hiding the data from the users or in other words we can say it protects the code by preventing access from &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/encapsulation\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Encapsulation With Example And Program In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-702","page","type-page","status-publish","hentry"],"psp_head":"<title>Encapsulation With Example And Program In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn Encapsulation concept with example and why we create private fields, getter and setter in Encapsulation. Also difference between Abstraction and Encapsulation with example.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/encapsulation\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=702"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/702\/revisions"}],"predecessor-version":[{"id":1470,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/702\/revisions\/1470"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}