{"id":21353,"date":"2016-10-27T08:33:54","date_gmt":"2016-10-27T08:33:54","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=21353"},"modified":"2025-05-16T14:38:26","modified_gmt":"2025-05-16T14:38:26","slug":"difference-between-an-abstract-class-and-an-interface","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/difference-between-an-abstract-class-and-an-interface\/","title":{"rendered":"Difference between an Abstract class and an Interface?"},"content":{"rendered":"<h1>Difference between an Abstract class and an Interface?<\/h1>\n<p>Let&#8217;s discuss some differences between an abstract class and an interface in Java. Interfaces provide the flexibility for unrelated classes to implement\u00a0 multiple interfaces.<\/p>\n<p>In Java, both abstract classes and interfaces are used to achieve abstraction \u2014 a core concept in object-oriented programming that allows you to hide complex implementation details and show only the necessary features of an object. However, many beginners get confused between the two because they seem similar but are used in different scenarios and have different rules. This tutorial will help you understand what abstract classes and interfaces are, and how they differ from each other.<\/p>\n<h2>What is an Abstract Class?<\/h2>\n<p>An abstract class in Java is a class that cannot be instantiated on its own and is meant to be extended by other classes. It can have both abstract methods (without implementation) and concrete methods (with implementation). Abstract classes are used when some common behavior should be inherited by multiple subclasses, but each subclass can have its own specific implementation for certain methods.<\/p>\n<ul>\n<li>Defined using the <code class=\"\" data-line=\"\">abstract<\/code> keyword.<\/li>\n<li>Can contain constructors and member variables.<\/li>\n<li>Can have both abstract and non-abstract methods.<\/li>\n<li>Used for providing partial abstraction.<\/li>\n<\/ul>\n<h2>What is an Interface?<\/h2>\n<p>An interface in Java is a completely abstract class that is used to specify a set of methods that a class must implement. It is like a contract that ensures certain behaviors are implemented by the class that chooses to implement the interface.<\/p>\n<ul>\n<li>Defined using the <code class=\"\" data-line=\"\">interface<\/code> keyword.<\/li>\n<li>Cannot contain constructors or instance variables (except <code class=\"\" data-line=\"\">static final<\/code> constants).<\/li>\n<li>All methods are implicitly abstract and public (before Java 8).<\/li>\n<li>Used for achieving full abstraction and multiple inheritance.<\/li>\n<\/ul>\n<h2>Abstract class vs Interface<\/h2>\n<p>Some of the differences are as follows:<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n<tbody>\n<tr>\n<td style=\"background-color: #efefef;\"><strong>Feature<\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Abstract Class<\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Interface<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Keyword Used<\/td>\n<td><code class=\"\" data-line=\"\">abstract<\/code><\/p>\n<p>Abstract Class is declared as :<\/p>\n<p><strong>public abstract class<\/strong> ClassName {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<td><code class=\"\" data-line=\"\">interface<\/code><\/p>\n<p>Interface is declared as :<\/p>\n<p><strong>public interface<\/strong> InterfaceName {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<tr>\n<td>Method Implementation<\/td>\n<td>Can have both abstract and concrete methods.<\/p>\n<p>Abstract Class can have abstract methods and may also have implemented methods. Abstract classes can define some default implementation and leave abstract method implementations to sub classes.<\/td>\n<td>Only abstract methods (default and static methods allowed from Java 8)<\/p>\n<p>Interface methods are by default abstract. There is no need to declare with keyword abstract. Interfaces only contain method signatures.<\/td>\n<\/tr>\n<tr>\n<td>Instantiation<\/td>\n<td>Cannot be instantiated<\/td>\n<td>Cannot be instantiated<\/td>\n<\/tr>\n<tr>\n<td>Inheritance<\/td>\n<td>Supports single inheritance<\/p>\n<p>Derived class can only extend on Abstract class in Java. Java doesn&#8217;t support multiple class inheritance. Derived classes of an abstract class directly come under the Abstract class in the class hierarchy.<\/td>\n<td>Supports multiple inheritance<\/p>\n<p>Java classes can implement multiple interfaces and from any where in the class hierarchy.<\/td>\n<\/tr>\n<tr>\n<td>Constructors<\/td>\n<td>Can have constructors<\/td>\n<td>Cannot have constructors<\/td>\n<\/tr>\n<tr>\n<td>Variables<\/td>\n<td>Can have instance variables<\/td>\n<td>Can have only <code class=\"\" data-line=\"\">public static final<\/code> constants<\/td>\n<\/tr>\n<tr>\n<td>Access Modifiers<\/td>\n<td>Can have any access modifier<\/td>\n<td>All methods are <code class=\"\" data-line=\"\">public<\/code> by default<\/td>\n<\/tr>\n<tr>\n<td>Use Case<\/td>\n<td>Used when classes share common behavior<\/td>\n<td>Used when classes must follow a contract<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Difference between an Abstract class and an Interface? Let&#8217;s discuss some differences between an abstract class and an interface in Java. Interfaces provide the flexibility for unrelated classes to implement\u00a0 multiple interfaces. In Java, both abstract classes and interfaces are used to achieve abstraction \u2014 a core concept in object-oriented programming that allows you to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-21353","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=21353"}],"version-history":[{"count":12,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21353\/revisions"}],"predecessor-version":[{"id":27338,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21353\/revisions\/27338"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=21353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=21353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=21353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}