<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://pratik05jha.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 00:47:33 GMT</lastBuildDate><atom:link href="https://pratik05jha.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Abstraction and interface in java]]></title><description><![CDATA[Abstraction
'abstract' is a non-access modifier in Java for classes and methods but not variables.
Characteristic of abstract keyword :

Can't create an object directly. You need to extend it by some other class.

The abstract method does not have a ...]]></description><link>https://pratik05jha.hashnode.dev/abstraction-and-interface-in-java</link><guid isPermaLink="true">https://pratik05jha.hashnode.dev/abstraction-and-interface-in-java</guid><category><![CDATA[abstract class vs interface]]></category><dc:creator><![CDATA[Pratik Kumar Jha]]></dc:creator><pubDate>Mon, 21 Aug 2023 17:00:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/95YRwf6CNw8/upload/07520154d87e702442fe66847d6e874f.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-abstraction">Abstraction</h3>
<p><em>'abstract'</em> is a <mark>non-access modifier</mark> in Java for classes and methods but not variables.</p>
<p>Characteristic of abstract keyword :</p>
<ul>
<li><p>Can't create an object directly. You need to extend it by some other class.</p>
</li>
<li><p>The abstract method does not have a body; a subclass needs to override it.</p>
</li>
<li><p>It can have both abstract and normal(concrete) abstract methods.</p>
</li>
<li><p>it can have constructors.</p>
</li>
<li><p>it can have instance variables.</p>
</li>
<li><p>it can implement interfaces.</p>
</li>
</ul>
<p>An example to demonstrate the use of the 'abstract' keyword.</p>
<pre><code class="lang-java"><span class="hljs-comment">//define an abstrcat class</span>
<span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span></span>{
<span class="hljs-function"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">m1</span><span class="hljs-params">()</span></span>; <span class="hljs-comment">//abstract method has no body</span>
<span class="hljs-comment">//normal methods are also allowed in abstract class</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">m2</span><span class="hljs-params">()</span></span>{
System.out.println(<span class="hljs-string">"this is an normal method"</span>);
};
}
<span class="hljs-comment">//normal class B </span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">A</span></span>{
    <span class="hljs-comment">//class B must override method of Aotherwise compile time error would be shown</span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">m1</span><span class="hljs-params">()</span></span>{
        System.out.println(<span class="hljs-string">"B's implementation of m1"</span>);
}
}
<span class="hljs-comment">//test class</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span> <span class="hljs-params">(String[] args)</span> </span>{
        B b=<span class="hljs-keyword">new</span> B();
        b.m1();
        b.m2();
            }
}
</code></pre>
<p>Note: it's compulsory to override your method otherwise you would get a compile-time error. Think of this as a rule.</p>
<h3 id="heading-interfaces">Interfaces</h3>
<p>Interfaces are similar to classes in many ways. Interfaces can be used to achieve 100% abstraction.</p>
<ul>
<li><p>there can only be abstract methods (which means "no content").</p>
</li>
<li><p>Can help in achieving multiple inheritance.</p>
</li>
<li><p>class extends class; class implements Interface; interface extends interface.</p>
</li>
<li><p>Updates -&gt; Since Java 8 we can have default and static methods.</p>
<p>  -&gt; Science Java 9 we can have private methods in an interface.</p>
<p>  An example to demonstrate the use of the 'Interface' keyword.</p>
<pre><code class="lang-java">  <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Bank</span>
  </span>{

      <span class="hljs-function"><span class="hljs-keyword">float</span> <span class="hljs-title">rateOfInterest</span> <span class="hljs-params">()</span></span>;

  }
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SBI</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Bank</span>
  </span>{

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">float</span> <span class="hljs-title">rateOfInterest</span> <span class="hljs-params">()</span>
      </span>{
          <span class="hljs-keyword">return</span> <span class="hljs-number">9.15f</span>;
      }

  }

  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PNB</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Bank</span>
  </span>{

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">float</span> <span class="hljs-title">rateOfInterest</span> <span class="hljs-params">()</span>
      </span>{
          <span class="hljs-keyword">return</span> <span class="hljs-number">9.7f</span>;
      }

  }

  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestInterface2</span>
  </span>{

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span> <span class="hljs-params">(String[]args)</span>
      </span>{

          Bank b = <span class="hljs-keyword">new</span> SBI ();

          System.out.println (<span class="hljs-string">"ROI: "</span> + b.rateOfInterest ());

     }
  }
</code></pre>
<p>  PS:- <strong>Modifiers</strong> are specific keywords present in Java using which we can make changes to the characteristics of a variable, method, or class and limit its scope.</p>
<p>  -&gt;Modifiers in Java are divided into two types – <strong>Access Modifiers</strong> and <strong>Non-Access modifiers</strong>.</p>
<p>  -&gt;Non-access modifiers provide information about the characteristics of a class, method, or variable to the JVM. ‘static’ , ‘abstract’, ‘final’ , ‘synchronized’ are some examples of non-access modifiers.</p>
</li>
<li><p>-&gt;Access Modifiers - 1. public (member can be accessed by any other code) 2. private (member can only be accessed by other members of its class) 3. default (no modifier) 4. protected (allows a member (method, variable, or constructor) to be accessed within its own class, by its subclasses, and by other classes within the same package)</p>
</li>
</ul>
<p>To the readers : If there is any further scope of improvement, then kindly bring to my attention. :)</p>
]]></content:encoded></item></channel></rss>