{"id":329,"date":"2025-06-30T07:21:40","date_gmt":"2025-06-30T07:21:40","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=329"},"modified":"2025-07-16T10:11:36","modified_gmt":"2025-07-16T10:11:36","slug":"difference-encapsulation-and-abstraction","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/difference-encapsulation-and-abstraction\/","title":{"rendered":"Difference Encapsulation and Abstraction ?"},"content":{"rendered":"\n<p>\ud83d\udd10 <strong>Encapsulation \u2014 \u201cProtect the Data\u201d<\/strong><\/p>\n\n\n\n<p>\u27a4 <strong>What It Is:<\/strong><\/p>\n\n\n\n<p>Encapsulation means <strong>wrapping data (variables) and methods (functions) together into a single unit<\/strong>, typically a class, and <strong>restricting direct access<\/strong> to some components using access modifiers (<code>private<\/code>, <code>public<\/code>, etc.).<\/p>\n\n\n\n<p>\ud83c\udfaf <strong>Purpose:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To <strong>hide internal object details<\/strong> from outside interference.<\/li>\n\n\n\n<li>To ensure <strong>control over data<\/strong> using getter and setter methods.<\/li>\n<\/ul>\n\n\n\n<p>\u2705<strong> Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class BankAccount {\n    private double balance; \/\/ Hidden from outside\n\n    public void deposit(double amount) {\n        if(amount > 0) balance += amount;\n    }\n\n    public void withdraw(double amount) {\n        if(amount > 0 &amp;&amp; amount &lt;= balance) balance -= amount;\n    }\n\n    public double getBalance() {\n        return balance;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Here, you can\u2019t access <code>balance<\/code> directly, only via controlled methods.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfad<strong> Abstraction \u2014 \u201cShow Only What\u2019s Necessary\u201d<\/strong><\/p>\n\n\n\n<p>\u27a4 What It Is:<\/p>\n\n\n\n<p>Abstraction means <strong>hiding implementation details<\/strong> and <strong>only exposing essential features<\/strong> of an object.<\/p>\n\n\n\n<p>You can achieve abstraction in Java using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Abstract classes<\/strong><\/li>\n\n\n\n<li><strong>Interfaces<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\ud83c\udfaf Purpose:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To <strong>focus on what an object does<\/strong>, not how it does it.<\/li>\n\n\n\n<li>Makes code more <strong>modular<\/strong>, <strong>extensible<\/strong>, and <strong>easier to understand<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>\u2705 Example using Interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Vehicle {\n    void start();   \/\/ Abstract method\n    void stop();\n}\n\nclass Car implements Vehicle {\n    public void start() {\n        System.out.println(\"Car starts with key\");\n    }\n    public void stop() {\n        System.out.println(\"Car stops with brake\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>The user of the <code>Vehicle<\/code> interface doesn\u2019t care how the <code>Car<\/code> starts or stops\u2014just that it does.<br><br><strong>\ud83e\udde0 Side-by-Side Comparison<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Encapsulation<\/th><th>Abstraction<\/th><\/tr><\/thead><tbody><tr><td><strong>Goal<\/strong><\/td><td>Hide data<\/td><td>Hide complexity<\/td><\/tr><tr><td><strong>Focus<\/strong><\/td><td>How you protect <em>data<\/em><\/td><td>How you hide <em>implementation logic<\/em><\/td><\/tr><tr><td><strong>Accessed via<\/strong><\/td><td>Getters and Setters<\/td><td>Abstract classes and Interfaces<\/td><\/tr><tr><td><strong>Achieved Using<\/strong><\/td><td><code>private<\/code> + <code>public<\/code> methods<\/td><td><code>abstract<\/code> keyword \/ interfaces<\/td><\/tr><tr><td><strong>Hides<\/strong><\/td><td>Object\u2019s internal state<\/td><td>Implementation complexity<\/td><\/tr><tr><td><strong>Used For<\/strong><\/td><td>Data hiding<\/td><td>Design flexibility<\/td><\/tr><tr><td><strong>Example<\/strong><\/td><td>Banking system with account balance<\/td><td>Interface for all types of vehicles<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn the difference between encapsulation and abstraction in Java. Discover how each concept secures data and simplifies design in object-oriented programming<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":425,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions\/425"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}