{"id":13478,"date":"2025-03-26T09:05:00","date_gmt":"2025-03-26T04:05:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=13478"},"modified":"2025-11-11T09:28:06","modified_gmt":"2025-11-11T04:28:06","slug":"abstract-classes-and-interfaces-designing-extensible-frameworks","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/26\/abstract-classes-and-interfaces-designing-extensible-frameworks\/","title":{"rendered":"Abstract Classes and Interfaces \u2014 Designing Extensible Frameworks"},"content":{"rendered":"\n<p>In our previous lecture, we explored <strong>virtual and pure virtual functions<\/strong>.<br>We saw how a <em>virtual function<\/em> allows different derived classes to perform the same action in their own way \u2014 like different employees performing their duties differently.<\/p>\n\n\n\n<p>We also discovered that a <em>pure virtual function<\/em> acts as a promise \u2014 a function that <strong>must<\/strong> be implemented by every derived class.<br>This is where <strong>abstract classes<\/strong> emerge.<\/p>\n\n\n\n<p>An abstract class can be imagined as a <strong>template or blueprint<\/strong> \u2014 it tells us <em>what must exist<\/em>, but not <em>how it works<\/em>.<br>It serves as a foundation for building <strong>extensible frameworks<\/strong>, where future developers can add new components without changing the existing code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Abstraction Is Needed<\/strong><\/h2>\n\n\n\n<p>In daily life, abstraction is everywhere.<br>Think of the <strong>employee structure<\/strong> in a large organization. Every employee \u2014 teacher, developer, or manager \u2014 follows the same base rules defined by the organization.<br>The organization acts as a <em>standard interface<\/em>, but what each device does with that electricity differs.<\/p>\n\n\n\n<p>This is the heart of abstraction:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201cDefine what should be done, but let others decide how it should be done.\u201d<\/p>\n<\/blockquote>\n\n\n\n<p>When designing software frameworks \u2014 whether for a school management system or a smart city simulator \u2014 abstraction ensures that the system remains open for growth.<br>New modules can join the system as long as they follow the <strong>same contract<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding Abstract Classes and Interfaces<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Abstract Class<\/h3>\n\n\n\n<p>An <strong>abstract class<\/strong> is a class that cannot be instantiated directly.<br>It may have both normal and pure virtual functions.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201cAn abstract class defines a contract for derived classes \u2014 it says <em>what<\/em> they must do but may partially define <em>how<\/em>.\u201d<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Interface<\/h3>\n\n\n\n<p>An <strong>interface<\/strong> is a special type of abstract class that has only pure virtual functions \u2014 no data, no implementation.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201cAn interface is a promise \u2014 it defines behavior but leaves the complete implementation to others.\u201d<\/p>\n<\/blockquote>\n\n\n\n<p>So in C++:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Every class with at least one pure virtual function is <em>abstract<\/em>.<\/li>\n\n\n\n<li>A class with <strong>only pure virtual functions<\/strong> is considered an <em>interface<\/em>.<\/li>\n<\/ul>\n\n\n\n<p>Previously, we created an example where an <code>Employee<\/code> class had a virtual function <code>performDuty()<\/code> that was implemented differently by each employee type (like <code>Teacher<\/code> and <code>Developer<\/code>).<\/p>\n\n\n\n<p>Now we will extend that same idea to understand <strong>abstract classes<\/strong> and <strong>interfaces<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Abstract Class Example<\/strong><\/h2>\n\n\n\n<p>Let\u2019s begin by making <code>Employee<\/code> an abstract class.<br>We\u2019ll add a pure virtual function to define a common responsibility that each employee must perform.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\n\/\/ Abstract Base Class\nclass Employee {\npublic:\n    virtual void performDuty() = 0;   \/\/ Pure virtual function\n    virtual void report() {           \/\/ Normal function\n        cout &lt;&lt; \"Employee reporting to the office.\" &lt;&lt; endl;\n    }\n};\n<\/code><\/pre>\n\n\n\n<p>The presence of <code>= 0<\/code> makes this function <em>pure virtual<\/em>, and thus, <code>Employee<\/code> becomes an abstract class.<br>It cannot be instantiated directly.<br>Any class that inherits from it must define its own version of <code>performDuty()<\/code>.<\/p>\n\n\n\n<p>Let\u2019s create two types of employees:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Teacher : public Employee {\npublic:\n    void performDuty() override {\n        cout &lt;&lt; \"Teacher is delivering a lecture to students.\" &lt;&lt; endl;\n    }\n};\n\nclass Developer : public Employee {\npublic:\n    void performDuty() override {\n        cout &lt;&lt; \"Developer is writing code for the new application.\" &lt;&lt; endl;\n    }\n};\n<\/code><\/pre>\n\n\n\n<p>Now, we can use <strong>polymorphism<\/strong> to handle all employees through a single interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main() {\n    Employee* e1 = new Teacher();\n    Employee* e2 = new Developer();\n\n    e1-&gt;performDuty();\n    e2-&gt;performDuty();\n\n    e1-&gt;report();  \/\/ Normal function from abstract base class\n\n    delete e1;\n    delete e2;\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d Output:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Teacher is delivering a lecture to students.\nDeveloper is writing code for the new application.\nEmployee reporting to the office.\n<\/code><\/pre>\n\n\n\n<p>In this setup:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>abstract class<\/strong> <code>Employee<\/code> defines what all employees must be able to do.<\/li>\n\n\n\n<li>Each subclass (<code>Teacher<\/code>, <code>Developer<\/code>) defines how that duty is done.<\/li>\n\n\n\n<li>This structure allows us to add more employee types (like <code>Designer<\/code>, <code>Manager<\/code>) without ever changing the original framework.<\/li>\n<\/ul>\n\n\n\n<p>This is <strong>extensibility<\/strong> \u2014 a fundamental goal in software architecture.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Interface: When We Need Only the Contract<\/strong><\/h2>\n\n\n\n<p>Sometimes we don\u2019t even need partial implementation \u2014 we just need to define behavior that different classes can \u201cagree\u201d to follow.<\/p>\n\n\n\n<p>For instance, in a school system, some employees receive payments.<br>Others may not (like unpaid interns).<br>Let\u2019s define an <strong>interface<\/strong> for that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Payable {\npublic:\n    virtual void processPayment() = 0;\n    virtual ~Payable() {} \/\/ Always define a virtual destructor in interfaces\n};\n<\/code><\/pre>\n\n\n\n<p>Now, <code>Teacher<\/code> and <code>Developer<\/code> can \u201cimplement\u201d this interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Teacher : public Employee, public Payable {\npublic:\n    void performDuty() override {\n        cout &lt;&lt; \"Teacher is delivering a lecture.\" &lt;&lt; endl;\n    }\n    void processPayment() override {\n        cout &lt;&lt; \"Teacher receives monthly salary.\" &lt;&lt; endl;\n    }\n};\n\nclass Developer : public Employee, public Payable {\npublic:\n    void performDuty() override {\n        cout &lt;&lt; \"Developer is writing project code.\" &lt;&lt; endl;\n    }\n    void processPayment() override {\n        cout &lt;&lt; \"Developer receives project-based payment.\" &lt;&lt; endl;\n    }\n};\n<\/code><\/pre>\n\n\n\n<p>And in <code>main()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main() {\n    Payable* p1 = new Teacher();\n    Payable* p2 = new Developer();\n\n    p1-&gt;processPayment();\n    p2-&gt;processPayment();\n\n    delete p1;\n    delete p2;\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d Output:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Teacher receives monthly salary.\nDeveloper receives project-based payment.\n<\/code><\/pre>\n\n\n\n<p>This structure allows one class to <strong>play multiple roles<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Employee<\/code> \u2192 defines what it means to be an employee (performing duties).<\/li>\n\n\n\n<li><code>Payable<\/code> \u2192 defines what it means to be paid (processing payments).<\/li>\n<\/ul>\n\n\n\n<p>C++ supports <strong>multiple inheritance<\/strong> safely in such interface scenarios because interfaces typically have no data \u2014 only pure virtual functions.<\/p>\n\n\n\n<p>This concept is used widely in real frameworks:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In <strong>game engines<\/strong>, an object can be both <code>Drawable<\/code> and <code>Updatable<\/code>.<\/li>\n\n\n\n<li>In <strong>network systems<\/strong>, a device can be both <code>Connectable<\/code> and <code>Rechargeable<\/code>.<\/li>\n\n\n\n<li>In <strong>simulation models<\/strong>, an entity can be both <code>Communicable<\/code> and <code>Mobile<\/code>.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Type<\/strong><\/th><th><strong>Can Be Instantiated<\/strong><\/th><th><strong>Contains Pure Virtual Functions<\/strong><\/th><th><strong>May Have Implementation<\/strong><\/th><th><strong>Typical Purpose<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Concrete Class<\/td><td>\u2705 Yes<\/td><td>\u274c No<\/td><td>\u2705 Yes<\/td><td>Fully defined behavior and data<\/td><\/tr><tr><td>Abstract Class<\/td><td>\u274c No<\/td><td>\u2705 At least one<\/td><td>\u2705 May have partial implementation<\/td><td>Base class defining structure for derived classes<\/td><\/tr><tr><td>Interface<\/td><td>\u274c No<\/td><td>\u2705 All<\/td><td>\u274c No<\/td><td>Defines contracts or roles that multiple classes can adopt<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Self-Assessment<\/strong><\/h2>\n\n\n\n<p><strong>Conceptual:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Can we create an object of an abstract class? Why not?<\/li>\n\n\n\n<li>How can we represent interfaces in C++?<\/li>\n\n\n\n<li>Why do interfaces often have virtual destructors?<\/li>\n\n\n\n<li>What\u2019s the advantage of having multiple interfaces?<\/li>\n<\/ol>\n\n\n\n<p><strong>Coding Task:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add a new class <code>ResearchAssistant<\/code> that:\n<ul class=\"wp-block-list\">\n<li>Inherits from both <code>Employee<\/code> and <code>Payable<\/code><\/li>\n\n\n\n<li>Adds a function <code>conductResearch()<\/code><\/li>\n\n\n\n<li>Implements all required functions and displays messages accordingly.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In our previous lecture, we explored virtual and pure virtual functions.We saw how a virtual function allows different derived classes to perform the same action in their own way \u2014 like different employees performing their duties differently. We also discovered that a pure virtual function acts as a promise \u2014 a function that must be implemented by every derived class.This is where abstract classes emerge. An abstract class can be imagined as a template or blueprint \u2014 it tells us&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/26\/abstract-classes-and-interfaces-designing-extensible-frameworks\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":13711,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[637],"tags":[640,726],"class_list":["post-13478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-abstract-classes","tag-interfaces"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-4.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-3vo","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/13478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=13478"}],"version-history":[{"count":2,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/13478\/revisions"}],"predecessor-version":[{"id":13715,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/13478\/revisions\/13715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/13711"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=13478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=13478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=13478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}