{"id":2360,"date":"2026-10-15T13:00:00","date_gmt":"2026-10-15T07:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/"},"modified":"2026-11-15T11:30:26","modified_gmt":"2026-11-15T06:00:26","slug":"apex-inheritance-polymorphism","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/","title":{"rendered":"Inheritance and Polymorphism in Apex Explained with Examples"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Inheritance lets a child class reuse the code of a parent class and change only the parts it needs. So a base notification class can hold the shared wording, while an SMS class changes just the length. Polymorphism is what makes one line of code run different versions depending on the real object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-interfaces-abstract-virtual-classes\/\">Apex Interfaces<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-classes-and-objects-constructors\/\">classes and objects<\/a>. We will keep using Bright Path Tutors for the examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Apex Inheritance?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Inheritance is when one class extends another and gets its methods and variables. The parent holds the shared code, and the child adds or replaces pieces. Apex supports single inheritance only, so a class has one parent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a family recipe for dal. The base recipe is the parent. Your version adds extra garlic, but everything else stays the same.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Inheritance Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Copying the same twenty lines into five classes means fixing bugs five times. With Apex Inheritance you write that code once in the parent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism then keeps the calling code simple. You hold a list of the parent type and call one method, and each object does its own thing. So adding a sixth type does not touch the loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Inheritance Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For Apex Inheritance, the parent must be marked <code>virtual<\/code> or <code>abstract<\/code>, or nothing can extend it. Methods you want a child to change must also be virtual or abstract. The child then uses <code>extends<\/code> and <code>override<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-1-1024x640.png\" alt=\"Apex Inheritance diagram showing how the runtime picks a child method\" class=\"wp-image-2358\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How the runtime picks the child version of a method.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">The Parent Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public virtual class Notice {\n    protected String student;\n    public Notice(String student) { this.student = student; }\n    \/\/ Children may replace this\n    public virtual String text() {\n        return 'Hello ' + student + ', your class starts soon.';\n    }\n    \/\/ Children cannot replace this\n    public String footer() { return 'Bright Path Tutors'; }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>text<\/code> method is virtual, so a child may change it. The <code>footer<\/code> method is plain, so it is fixed for everyone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Child Classes<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SmsNotice extends Notice {\n    public SmsNotice(String student) { super(student); }\n    public override String text() {\n        return 'Hi ' + student + ', class in 30 mins.';\n    }\n}\n\npublic class EmailNotice extends Notice {\n    public EmailNotice(String student) { super(student); }\n    public override String text() {\n        return 'Dear ' + student + ', this is a reminder for your class today.';\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each child calls <code>super<\/code> to run the parent constructor. Then it replaces only the <code>text<\/code> method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Polymorphism in Action<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The loop below is written against <code>Notice<\/code>, yet each object runs its own version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Notice&gt; all = new List&lt;Notice&gt;{\n    new SmsNotice('Asha'),\n    new EmailNotice('Ravi'),\n    new Notice('Neha')\n};\nfor (Notice n : all) {\n    System.debug(n.text() + ' | ' + n.footer());\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The log shows three different messages from one call.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hi Asha, class in 30 mins. | Bright Path Tutors\nDear Ravi, this is a reminder for your class today. | Bright Path Tutors\nHello Neha, your class starts soon. | Bright Path Tutors<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Checking the Real Type<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes you need to know which child you have. Apex gives you <code>instanceof<\/code> and casting.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (Notice n : all) {\n    if (n instanceof SmsNotice) {\n        SmsNotice s = (SmsNotice) n;\n        System.debug('This one is an SMS: ' + s.text());\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use this sparingly. Long chains of <code>instanceof<\/code> usually mean the parent needs another virtual method instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Inheritance Keyword Rules<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This table lists what each keyword does.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Keyword<\/th><th>Used on<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>virtual<\/td><td>Class or method<\/td><td>May be extended or overridden<\/td><\/tr><tr><td>abstract<\/td><td>Class or method<\/td><td>Must be extended or overridden<\/td><\/tr><tr><td>override<\/td><td>Child method<\/td><td>Replaces the parent version<\/td><\/tr><tr><td>super<\/td><td>Child code<\/td><td>Calls the parent constructor or method<\/td><\/tr><tr><td>extends<\/td><td>Child class<\/td><td>Names the single parent<\/td><\/tr><tr><td>protected<\/td><td>Member<\/td><td>Visible to this class and its children<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Overriding vs Overloading<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These two words sound alike but mean different things. This table compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Overriding<\/th><th>Overloading<\/th><\/tr><\/thead><tbody><tr><td>Classes involved<\/td><td>Parent and child<\/td><td>One class<\/td><\/tr><tr><td>Signature<\/td><td>Identical<\/td><td>Different parameters<\/td><\/tr><tr><td>Keywords<\/td><td>virtual and override<\/td><td>None<\/td><\/tr><tr><td>Chosen<\/td><td>At runtime<\/td><td>At compile time<\/td><\/tr><tr><td>Purpose<\/td><td>Change behaviour<\/td><td>Accept different inputs<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-2-1024x640.png\" alt=\"Apex Inheritance comparison of overriding and overloading\" class=\"wp-image-2359\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Overriding compared with overloading in Apex.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, overriding changes what a method does. Overloading gives one name several input shapes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Inheritance Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Bright Path sends fee receipts. Every receipt shares a header and total, but the wording differs by payment type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Build the Base<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public abstract class Receipt {\n    protected Decimal amount;\n    public Receipt(Decimal amount) { this.amount = amount; }\n    public abstract String method();\n    \/\/ Shared template using the child's method name\n    public String render() {\n        return 'Receipt for ' + amount + ' paid by ' + method() + '.';\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>render<\/code> method calls <code>method()<\/code>, which only the child knows. That pattern is called a template method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Add Two Children<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CashReceipt extends Receipt {\n    public CashReceipt(Decimal a) { super(a); }\n    public override String method() { return 'cash at the centre'; }\n}\n\npublic class UpiReceipt extends Receipt {\n    public UpiReceipt(Decimal a) { super(a); }\n    public override String method() { return 'UPI transfer'; }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Run It<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>System.debug(new CashReceipt(5000).render());\nSystem.debug(new UpiReceipt(5000).render());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output shows the shared sentence with different endings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Receipt for 5000 paid by cash at the centre.\nReceipt for 5000 paid by UPI transfer.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These are the classes we built.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Class<\/th><th>Type<\/th><th>Job<\/th><\/tr><\/thead><tbody><tr><td>Receipt<\/td><td>Abstract<\/td><td>Holds the shared sentence<\/td><\/tr><tr><td>CashReceipt<\/td><td>Child<\/td><td>Names cash<\/td><\/tr><tr><td>UpiReceipt<\/td><td>Child<\/td><td>Names UPI<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Inheritance<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Forgetting virtual on the parent class, so nothing can extend it.<\/li>\n\n<li>Leaving out override, which Apex requires on every replaced method.<\/li>\n\n<li>Trying to extend two classes, which Apex does not allow.<\/li>\n\n<li>Making fields private when children need them, instead of protected.<\/li>\n\n<li>Writing long instanceof chains instead of adding a virtual method.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Inheritance<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep the inheritance chain shallow, usually one or two levels.<\/li>\n\n<li>Put only truly shared code in the parent.<\/li>\n\n<li>Prefer an interface when classes share no real code.<\/li>\n\n<li>Use protected, not public, for members children need.<\/li>\n\n<li>Test the parent and every child, since coverage does not carry over.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a virtual class Greeting with a virtual method <code>line()<\/code>. Add HindiGreeting and EnglishGreeting children. Then loop a List of Greeting and print each line. Hint: do not forget override.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Apex Inheritance Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Apex Inheritance questions appear in most Salesforce developer interviews.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Apex Inheritance Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is inheritance in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Inheritance is when a child class extends a parent and reuses its methods and variables. The child can add or replace behaviour.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: Which keyword creates a child class?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Inheritance uses the extends keyword, followed by the single parent class name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: Does Apex support multiple inheritance?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. A class extends one parent only. You implement several interfaces instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What must a parent class be marked as?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It must be virtual or abstract. Otherwise Apex Inheritance is blocked, because a plain class cannot be extended.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: What is polymorphism?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is one call that behaves differently depending on the real object behind the variable. So Apex Inheritance and polymorphism usually appear together.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: What does super do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It calls the parent constructor, or a parent version of a method, from the child.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: Is override optional in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Apex requires the override keyword on every method that replaces a parent method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: What does protected mean?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The member is visible inside the class and inside its child classes, but not outside.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intermediate Apex Inheritance Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: What is the difference between overriding and overloading?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Overriding replaces a parent method with the same signature. Overloading defines several methods with the same name but different parameters in one class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Can you override a static method?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Static methods belong to the class, not the object, so they cannot be overridden.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Can a constructor be virtual?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Constructors are not inherited and cannot be marked virtual or overridden.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: What is instanceof used for?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It checks whether an object is of a given type before you cast it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: What happens if you cast to the wrong child type?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You get a runtime TypeException. So check with instanceof first.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: Can an abstract class extend a virtual class?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. An abstract class can extend another class and add abstract methods of its own.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: Does test coverage of the parent cover the child?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Each class needs its own tests, since coverage is measured per class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: What is the template method pattern?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The parent writes the overall steps and calls abstract methods that children fill in.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Apex Inheritance Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: When would you prefer composition over inheritance?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When classes share behaviour but are not truly the same kind of thing. Passing in a helper object is more flexible than extending.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: Scenario: a trigger handler base class grows to 500 lines. What would you change?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Move the shared parts into small helper classes and keep the base thin. Deep hierarchies get hard to test.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How does inheritance interact with sharing keywords?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A class declared with or without sharing passes that mode to classes it calls, and inherited sharing takes the caller&#8217;s mode. So state it explicitly on the parent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Can a child reduce the visibility of a parent method?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. The override must keep the same signature and cannot be less visible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you mock a parent class in a test?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mark it virtual and use Test.createStub, or extend it with a test-only child that returns fixed values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: Scenario: you must add a new receipt type weekly. Inheritance or interface?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If every type shares a real rendering template, an abstract parent fits. If they only share a contract, use an interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: What is the diamond problem and does Apex have it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is the ambiguity from inheriting the same method through two parents. Apex avoids it by allowing one parent only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: How deep should an inheritance tree go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One or two levels in most Salesforce code. Deeper trees make the real behaviour hard to trace.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Does Apex Inheritance apply to triggers?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not directly, because a trigger is not a class. However, trigger handler classes often use a base handler, so the pattern still helps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: Can an interface and Apex Inheritance be combined?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A class can extend one parent and implement several interfaces at the same time. That is a common way to get both shared code and a contract.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: What happens to private parent fields in a child?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The child cannot see them. So mark members protected when children need access.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: How does Apex Inheritance affect governor limits?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not change limits by itself. The same queries and DML count wherever they run, so watch loops in parent methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: Scenario: a child needs the parent logic plus extra steps. How do you write it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Override the method, call super first to run the parent version, then add your extra steps after it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q30: Why avoid deep inheritance in managed packages?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because subscribers cannot change your classes, and deep chains make upgrades risky. Shallow designs are safer to ship.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_classes_extending.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Extending a Class<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_classes_keywords.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Keywords<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_basics_dotnet\" target=\"_blank\" rel=\"noopener noreferrer\">Trailhead: Object-Oriented Programming for Admins<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Inheritance shares parent code with child classes.<\/li>\n\n<li>The parent must be virtual or abstract to be extended.<\/li>\n\n<li>Children use extends, override and super.<\/li>\n\n<li>Polymorphism makes one call run the right child version.<\/li>\n\n<li>Keep hierarchies shallow and test every class.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we look at <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-wrapper-inner-classes\/\">Wrapper Classes and Inner Classes<\/a>, which package several values into one object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Inheritance lets a child class reuse the code of a parent class and change only the parts it needs. So a base notification&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2357,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[239,8,96],"tags":[98,34,26],"swapz_series":[],"class_list":["post-2360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apex-fundamentals","category-salesforce","category-salesforce-developer","tag-apex","tag-certification","tag-intermediate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Inheritance and Polymorphism Explained<\/title>\n<meta name=\"description\" content=\"Learn Apex Inheritance and polymorphism with parent and child classes, override rules, casting and instanceof, plus top developer interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Inheritance and Polymorphism Explained\" \/>\n<meta property=\"og:description\" content=\"Learn Apex Inheritance and polymorphism with parent and child classes, override rules, casting and instanceof, plus top developer interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-15T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:00:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Inheritance and Polymorphism in Apex Explained with Examples\",\"datePublished\":\"2026-10-15T07:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/\"},\"wordCount\":1605,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inheritance-polymorphism-featured.png\",\"keywords\":[\"Apex\",\"Certification\",\"Intermediate\"],\"articleSection\":[\"Apex Fundamentals\",\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/\",\"name\":\"Apex Inheritance and Polymorphism Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inheritance-polymorphism-featured.png\",\"datePublished\":\"2026-10-15T07:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:26+00:00\",\"description\":\"Learn Apex Inheritance and polymorphism with parent and child classes, override rules, casting and instanceof, plus top developer interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inheritance-polymorphism-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-inheritance-polymorphism-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Inheritance featured image: code editor card with parent class\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-inheritance-polymorphism\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inheritance and Polymorphism in Apex Explained with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Inheritance and Polymorphism Explained","description":"Learn Apex Inheritance and polymorphism with parent and child classes, override rules, casting and instanceof, plus top developer interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/","og_locale":"en_US","og_type":"article","og_title":"Apex Inheritance and Polymorphism Explained","og_description":"Learn Apex Inheritance and polymorphism with parent and child classes, override rules, casting and instanceof, plus top developer interview questions.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-15T07:30:00+00:00","article_modified_time":"2026-11-15T06:00:26+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Inheritance and Polymorphism in Apex Explained with Examples","datePublished":"2026-10-15T07:30:00+00:00","dateModified":"2026-11-15T06:00:26+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/"},"wordCount":1605,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-featured.png","keywords":["Apex","Certification","Intermediate"],"articleSection":["Apex Fundamentals","Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/","name":"Apex Inheritance and Polymorphism Explained","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-featured.png","datePublished":"2026-10-15T07:30:00+00:00","dateModified":"2026-11-15T06:00:26+00:00","description":"Learn Apex Inheritance and polymorphism with parent and child classes, override rules, casting and instanceof, plus top developer interview questions.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-inheritance-polymorphism-featured.png","width":2400,"height":1260,"caption":"Apex Inheritance featured image: code editor card with parent class"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-inheritance-polymorphism\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Inheritance and Polymorphism in Apex Explained with Examples"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2360"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2360\/revisions"}],"predecessor-version":[{"id":3058,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2360\/revisions\/3058"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2357"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2360"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}