<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/abstract-class-vs-interfaces.png" border="0" /></div>
<p>This is very common question in core java interview for entry level programmer so let&#8217;s discuss some important point of difference as below:</p>
<h2>Difference between interfaces and abstract classes</h2>
<table border="1" align="center">
<tbody>
<tr>
<th scope="col">abstract class</th>
<th scope="col">interface</th>
</tr>
<tr>
<td>Abstract class may not be contain abstarct method or may be contain. we can say it can have abstract and non-abstract methods.</td>
<td>By default all methods of interface are abstract.</td>
</tr>
<tr>
<td>An abstract class may contain non-final variables.</td>
<td>Variables declared in a Java interface is by default final.</td>
</tr>
<tr>
<td>A Java abstract class can have the any flavors of class members like private, abstract.</td>
<td>Members of a Java interface are public by default.</td>
</tr>
<tr>
<td>An abstract class required when we want some default behavior in our project as like in DAO layer there are some common behavior save object, delete object etc.</td>
<td>Java interface is required when we want implement contract between service and client.</td>
</tr>
<tr>
<td>The abstract keyword is used to declare abstract class.<br />
<b>Example: </b><br />
public abstract class Shape{<br />
public abstract void draw();<br />
}</td>
<td>The interface keyword is used to declare interface.<br />
<b>Example:</b><br />
public interface Drawable{<br />
void draw();<br />
}</td>
</tr>
<tr>
<td>A Java abstract class should be extended using keyword &#8220;extends&#8221;.<br />
<b>Example: </b><br />
public class Circle extends Shape{<br />
public void draw(){<br />
//draw circle;<br />
}<br />
}</td>
<td>Java interface should be implemented using keyword &#8220;implements&#8221;;<br />
<b>Example: </b><br />
public class Circle implements Drawable{<br />
public void draw(){<br />
//draw circle;<br />
}<br />
}</td>
</tr>
<tr>
<td>Abstract class can have static methods, main method and constructor.</td>
<td>Interface can&#8217;t have static methods, main method or constructor.</td>
</tr>
<tr>
<td>Abstract class doesn&#8217;t support multiple inheritance.</td>
<td>Interface supports multiple inheritance.</td>
</tr>
</tbody>
</table>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<h2>
<b>Abstract Class Examples in Java &#8211;</b></h2>
<pre class="highlight">abstract class Shape{ 
 abstract void draw(); 
} 
 
class Rectangle extends Shape{ 
 void draw(){ 
 System.out.println("Drawing Rectangle"); 
 } 
} 
 
class Traingle extends Shape{ 
 void draw(){ 
 System.out.println("Drawing Traingle"); 
 } 
} 
 
class AbstractDemo{ 
 public static void main(String args[]){ 
 Shape s1 = new Rectangle(); 
 s1.draw(); 
 s1 = new Traingle(); 
 s1.draw(); 
 } 
} 
</pre>
<p><b>Output:</b><br />
<i>Drawing Rectangle<br />
Drawing Traingle.</i><br />
<b></b></p>
<h2><b>Interface Examples in Java &#8211;</b></h2>
<pre class="highlight">interface Drawable{ 
 abstract void draw(); 
 abstract void fillColor(); 
} 
 
class abstract BlueShape implement Drawable{ 
 void fillColor(){ 
 System.out.println("Fill Blue color in Shape"); 
 } 
} 
 
class Traingle extends BlueShape { 
 void draw(){ 
 System.out.println("Drawing Blue Traingle"); 
 } 
} 
 
class InterfaceDemo{ 
 public static void main(String args[]){ 
 Drawable s1 = new Traingle(); 
 s1.draw(); 
 } 
} 
</pre>
<h2><b>Output:</b></h2>
<p><i>Drawing Blue Traingle.</i></p>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/hashcode-and-equals-methods-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/core-java-interview-questions/">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: '82'});
});
</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…