{"id":7650,"date":"2025-08-27T13:26:34","date_gmt":"2025-08-27T07:56:34","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=what-is-the-difference-between-abstract-class-and-an-interface"},"modified":"2025-08-27T13:26:34","modified_gmt":"2025-08-27T07:56:34","slug":"what-is-the-difference-between-abstract-class-and-an-interface","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=what-is-the-difference-between-abstract-class-and-an-interface","title":{"rendered":"What is the difference between abstract class and an interface ?"},"content":{"rendered":"<h5><strong>Abstract Class<\/strong><\/h5>\n<ul>\n<li>Defined with <code>CLASS ... DEFINITION ABSTRACT<\/code>.<\/li>\n<li>Can have:\n<ul>\n<li><strong>Attributes (data)<\/strong><\/li>\n<li><strong>Concrete methods<\/strong> (implemented)<\/li>\n<li><strong>Abstract methods<\/strong> (declared but not implemented \u2014 must be redefined in subclass)<\/li>\n<\/ul>\n<\/li>\n<li>Cannot be instantiated directly \u2014 only subclasses can be created.<\/li>\n<li>Supports <strong>inheritance<\/strong> (a subclass inherits implementation + attributes).<\/li>\n<li>Can implement <strong>interfaces<\/strong> too.<\/li>\n<\/ul>\n<p>Think of an abstract class as a <strong>partially implemented blueprint<\/strong>.<\/p>\n<\/p>\n<p><strong>Example :<\/strong><\/p>\n<p>CLASS cl_vehicle DEFINITION ABSTRACT.<br \/>\nPUBLIC SECTION.<br \/>\nMETHODS: start ABSTRACT, &#8221; must be implemented in subclass<br \/>\nstop. &#8221; already implemented<br \/>\nENDCLASS.<\/p>\n<p>CLASS cl_vehicle IMPLEMENTATION.<br \/>\nMETHOD stop.<br \/>\nWRITE: \/ &#8216;Vehicle stopped.&#8217;.<br \/>\nENDMETHOD.<br \/>\nENDCLASS.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong>Interface<\/strong><\/h5>\n<ul>\n<li>Defined with <code>INTERFACE ...<\/code>.<\/li>\n<li>Can contain:<\/li>\n<li>Method declarations only (no implementation).<\/li>\n<li>Constants, types, attributes (but no data storage).<\/li>\n<li>No concrete code inside (implementation happens in the class that implements it).<\/li>\n<li>A class can implement multiple interfaces (like multiple inheritance).<\/li>\n<li>Cannot be instantiated or inherited \u2014 only implemented.<\/li>\n<\/ul>\n<p>Think of an interface as a <strong>contract<\/strong>: <em>\u201cany class implementing me must provide these methods.\u201d<\/em><\/p>\n<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>INTERFACE if_flyable.<br \/>\nMETHODS fly.<br \/>\nENDINTERFACE.<\/p>\n<p>INTERFACE if_swimmable.<br \/>\nMETHODS swim.<br \/>\nENDINTERFACE.<\/p>\n<p><strong>Key Differences:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th><strong>Abstract Class<\/strong><\/th>\n<th><strong>Interface<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Instantiation<\/strong><\/td>\n<td>Cannot be instantiated<\/td>\n<td>Cannot be instantiated<\/td>\n<\/tr>\n<tr>\n<td><strong>Inheritance<\/strong><\/td>\n<td>Single inheritance only<\/td>\n<td>Multiple interfaces can be implemented<\/td>\n<\/tr>\n<tr>\n<td><strong>Implementation<\/strong><\/td>\n<td>Can have <strong>abstract + concrete methods<\/strong><\/td>\n<td>Only method <strong>signatures<\/strong>, no code<\/td>\n<\/tr>\n<tr>\n<td><strong>Attributes<\/strong><\/td>\n<td>Yes (instance &amp; static data possible)<\/td>\n<td>Only <strong>constants\/types<\/strong>, no data<\/td>\n<\/tr>\n<tr>\n<td><strong>Purpose<\/strong><\/td>\n<td>Provide a <strong>base class with shared logic<\/strong><\/td>\n<td>Define a <strong>contract for behavior<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h5>When to Use<\/h5>\n<ul>\n<li>\n<p>Use an <strong>abstract class<\/strong> when:<\/p>\n<ul>\n<li>\n<p>You want to share <strong>implementation<\/strong> across subclasses.<\/p>\n<\/li>\n<li>\n<p>You want to define <strong>default behavior<\/strong> that can be reused.<\/p>\n<\/li>\n<li>\n<p>Example: <code>CL_VEHICLE<\/code> (base) \u2192 <code>CL_CAR<\/code>, <code>CL_TRUCK<\/code>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Use an <strong>interface<\/strong> when:<\/p>\n<ul>\n<li>\n<p>You just need a <strong>common contract<\/strong> across unrelated classes.<\/p>\n<\/li>\n<li>\n<p>You want to achieve <strong>multiple inheritance<\/strong> of behavior.<\/p>\n<\/li>\n<li>\n<p>Example: <code>IF_FLYABLE<\/code>, <code>IF_SWIMMABLE<\/code>, <code>IF_SERIALIZABLE<\/code>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,379,263,168,377,177],"class_list":["post-7650","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-abstract-class","question_tags-interface","question_tags-ooabap","question_tags-oop","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7650"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7650"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7650"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}