{"id":14796,"date":"2025-02-10T22:18:38","date_gmt":"2025-02-10T22:18:38","guid":{"rendered":"https:\/\/stackify.com\/?p=14796"},"modified":"2025-02-10T22:18:38","modified_gmt":"2025-02-10T22:18:38","slug":"oop-concept-polymorphism","status":"publish","type":"post","link":"https:\/\/stackify.com\/oop-concept-polymorphism\/","title":{"rendered":"OOP Concepts for Beginners: What Is Polymorphism"},"content":{"rendered":"\n<p><em>Object-Oriented Programming has different concepts allowing developers to build logical code. One of these concepts is polymorphism. But what is polymorphism?<\/em><\/p>\n\n\n\n<p>Polymorphism is one of the <a href=\"https:\/\/stackify.com\/oops-concepts-in-java\/\">core concepts of object-oriented programming (OOP)<\/a>&nbsp;that describes situations in which something occurs in several different forms. In computer science, polymorphism describes the concept that you can access objects of different types through the same interface. Each type can provide its own independent implementation of this interface.<\/p>\n\n\n\n<p>You can perform a simple test to know whether an object is polymorphic. If the object successfully passes multiple is-aor&nbsp;<a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/nutsandbolts\/op2.html\">instanceof tests<\/a>, it\u2019s polymorphic. As described in our post about&nbsp;<a href=\"https:\/\/stackify.com\/oop-concept-inheritance\/\">inheritance<\/a>, all Java classes extend the class&nbsp;<em>Object<\/em>. Due to this, all objects in&nbsp;<a href=\"https:\/\/stackify.com\/content\/java\/\">Java<\/a>&nbsp;are polymorphic because they pass at least two instanceofchecks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Different Types of Polymorphism<\/h2>\n\n\n\n<p>Java supports 2 types of polymorphism:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>-static or compile-time<\/li>\n\n\n\n<li>-dynamic<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Static Polymorphism<\/h3>\n\n\n\n<p>Like many other OOP languages, Java allows you to implement multiple methods within the same class that use the same name. But Java uses a different set of parameters called <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/methods.html\">method overloading<\/a>&nbsp;and represents a static form of polymorphism.<\/p>\n\n\n\n<p>The parameter sets have to differ in at least one of the following three criteria:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>-They need to have a different number of parameters, one method accepting 2 and another one accepting 3 parameters<\/li>\n\n\n\n<li>-The types of parameters need to be different, with one method accepting a <em>String<\/em>&nbsp;and another one accepting a&nbsp;<em>Long<\/em><\/li>\n\n\n\n<li>-They need to expect the parameters in a different order. For example, one method accepts a <em>String<\/em>&nbsp;and a&nbsp;<em>Long,<\/em>&nbsp;and another one accepts a&nbsp;<em>Long<\/em>&nbsp;and a&nbsp;<em>String<\/em>. This kind of overloading is not advisable because it makes the API difficult to understand<\/li>\n<\/ul>\n\n\n\n<p>In most cases, these overloaded methods provide a different but very similar functionality.<\/p>\n\n\n\n<p>Like many other OOP languages, Java allows you to implement multiple methods within the same class that use the same name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"200\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Like-many-other-OOP-languages-Java-allows-you-to-implement-multiple-methods-within-the-same-class-that-use-the-same-name.png\" alt=\"Like many other OOP languages, Java allows you to implement multiple methods within the same class that use the same name.\" class=\"wp-image-45266\" srcset=\"https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Like-many-other-OOP-languages-Java-allows-you-to-implement-multiple-methods-within-the-same-class-that-use-the-same-name.png 1000w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Like-many-other-OOP-languages-Java-allows-you-to-implement-multiple-methods-within-the-same-class-that-use-the-same-name-300x60.webp 300w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Like-many-other-OOP-languages-Java-allows-you-to-implement-multiple-methods-within-the-same-class-that-use-the-same-name-150x30.webp 150w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Like-many-other-OOP-languages-Java-allows-you-to-implement-multiple-methods-within-the-same-class-that-use-the-same-name-768x154.webp 768w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Like-many-other-OOP-languages-Java-allows-you-to-implement-multiple-methods-within-the-same-class-that-use-the-same-name-600x120.webp 600w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Due to the different sets of parameters, each method has a different&nbsp;<a href=\"https:\/\/www.thoughtco.com\/method-signature-2034235\">signature<\/a>. That signature allows the compiler to identify which method to call and binds it to the method call. This approach is popular, and it is known as static binding or static polymorphism.<\/p>\n\n\n\n<p>Let\u2019s take a look at an example.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">A Simple Example of Static Polymorphism<\/h4>\n\n\n\n<p>Let\u2019s use the same CoffeeMachine project as we used in the previous posts of this series. You can clone it at&nbsp;<a href=\"https:\/\/github.com\/thjanssen\/Stackify-OopInheritance\">Github<\/a>.<\/p>\n\n\n\n<p>The&nbsp;<em>BasicCoffeeMachine<\/em>&nbsp;class implements two methods with the name&nbsp;<em>brewCoffee<\/em>. The first one accepts one parameter of type&nbsp;<em>CoffeeSelection<\/em>. The other method accepts two parameters, a&nbsp;<em>CoffeeSelection<\/em>&nbsp;and an&nbsp;<em>int<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class BasicCoffeeMachine {\n    \/\/ ...\n    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException {\n        switch (selection) {\n        case FILTER_COFFEE:\n            return brewFilterCoffee();\n        default:\n            throw new CoffeeException(\n                \"CoffeeSelection &#91;\"+selection+\"] not supported!\");\n        }   \n    }\n  \n    public List brewCoffee(CoffeeSelection selection, int number) throws CoffeeException {\n        List coffees = new ArrayList(number);\n        for (int i=0; i&lt;number; i++) {\n            coffees.add(brewCoffee(selection));\n        }\n        return coffees;\n    }\n    \/\/ ...\n}\n<\/code><\/pre>\n\n\n\n<p>When you call one of these methods, the provided set of parameters identifies the method which has to be called.<\/p>\n\n\n\n<p>In the following code snippet, we\u2019ll call the method only with a&nbsp;<em>CoffeeSelection<\/em>&nbsp;object. At compile time, the Java compiler binds this method call to the&nbsp;<em>brewCoffee(CoffeeSelection selection)<\/em>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BasicCoffeeMachine coffeeMachine = createCoffeeMachine();\ncoffeeMachine.brewCoffee(CoffeeSelection.FILTER_COFFEE);\n<\/code><\/pre>\n\n\n\n<p>If we change this code and call the&nbsp;<em>brewCoffee<\/em>&nbsp;method with a&nbsp;<em>CoffeeSelection<\/em>&nbsp;object and an&nbsp;<em>int<\/em>, the compiler binds the method call to the other&nbsp;<em>brewCoffee(CoffeeSelection selection, int number)<\/em>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BasicCoffeeMachine coffeeMachine = createCoffeeMachine();\nList coffees = coffeeMachine.brewCoffee(CoffeeSelection.ESPRESSO, 2);\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Disadvantages of Static Method Dispatch<\/h4>\n\n\n\n<p>Although static method dispatch can be useful, it has some limitations that can cause problems in certain situations.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>No Flexibility at Runtime:<\/strong> With static method dispatch, the method to be called is determined when the program is compiled, not when it&#8217;s running. This means you can&#8217;t change the method behavior based on the actual object type during execution, which limits the flexibility of the code.<\/li>\n\n\n\n<li><strong>Can&#8217;t Take Advantage of Inheritance:<\/strong> If a subclass overrides a method, static dispatch will still call the method from the class where the call is made, not the overridden version in the subclass. This means you can&#8217;t fully use the power of inheritance and method overriding.<\/li>\n\n\n\n<li><strong>More Code Duplication:<\/strong> Since static dispatch doesn\u2019t allow for dynamic method selection, developers often end up writing separate methods for each class or type of object. This leads to repeated code, making the program harder to maintain.<\/li>\n\n\n\n<li><strong>Doesn&#8217;t Work Well with Interfaces:<\/strong> Static dispatch doesn&#8217;t support interfaces as well as dynamic dispatch. Since interfaces are often used to enable polymorphism, static dispatch limits the ability to easily change behavior based on the object at runtime.<\/li>\n\n\n\n<li><strong>Harder to Extend the Code:<\/strong> Static dispatch makes it harder to add new features or change the way the program works. Once a method is statically bound to a specific class, adding new functionality can require a lot of extra work, which reduces the program\u2019s ability to grow easily.<\/li>\n<\/ol>\n\n\n\n<p>Because of these limitations, many developers turn to <strong>dynamic method dispatch<\/strong>, which allows methods to be selected at runtime, providing more flexibility and power for working with objects<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-dynamic-polymorphism\"><strong>Dynamic Polymorphism<\/strong><\/h3>\n\n\n\n<p>This form of polymorphism doesn\u2019t allow the compiler to determine the executed method. The&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_virtual_machine\">JVM<\/a>&nbsp;needs to do that at runtime.<\/p>\n\n\n\n<p>Within an inheritance hierarchy, a subclass can override a method of its superclass, enabling the developer of the subclass to customize or completely replace the behavior of that method.<\/p>\n\n\n\n<p>Doing so also creates a form of polymorphism. Both methods implemented by the super- and subclasses share the same name and parameters. However, they provide different functionality.<\/p>\n\n\n\n<p>Let\u2019s take a look at another example from the CoffeeMachine project.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-method-overriding-in-an-inheritance-hierarchy\"><strong>Method Overriding in an Inheritance Hierarchy<\/strong><\/h4>\n\n\n\n<p>The&nbsp;<em>BasicCoffeeMachine<\/em>&nbsp;class is the superclass of the&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/12\/word-image-13.png\" alt=\"OOP Concepts for Beginners: What is Polymorphism Method overriding in an inheritance hierarchy - an example\" class=\"wp-image-14812\"\/><\/figure>\n\n\n\n<p>Both classes provide an implementation of the&nbsp;<em>brewCoffee(CoffeeSelection selection)<\/em>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\nimport java.util.List;\nimport java.util.Map;\n\npublic class BasicCoffeeMachine extends AbstractCoffeeMachine {\n    protected Map beans;\n    protected Grinder grinder;\n    protected BrewingUnit brewingUnit;\n\n    public BasicCoffeeMachine(Map beans) {\n        super();\n        this.beans = beans;\n        this.grinder = new Grinder();\n        this.brewingUnit = new BrewingUnit();\n\n        this.configMap.put(CoffeeSelection.FILTER_COFFEE, new Configuration(30, 480));\n    }\n\n    public List brewCoffee(CoffeeSelection selection, int number) throws CoffeeException {\n        List coffees = new ArrayList(number);\n        for (int i=0; i&lt;number; i++) {\n            coffees.add(brewCoffee(selection));\n        }\n        return coffees;\n    }\n\n    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException {\n        switch (selection) {\n        case FILTER_COFFEE:\n            return brewFilterCoffee();\n        default:\n            throw new CoffeeException(\"CoffeeSelection &#91;\"+selection+\"] not supported!\");\n        }\n    }\n\n    private Coffee brewFilterCoffee() {\n        Configuration config = configMap.get(CoffeeSelection.FILTER_COFFEE);\n\n        \/\/ grind the coffee beans\n        GroundCoffee groundCoffee = this.grinder.grind(this.beans.get(CoffeeSelection.FILTER_COFFEE), config.getQuantityCoffee());\n\n        \/\/ brew a filter coffee\n        return this.brewingUnit.brew(CoffeeSelection.FILTER_COFFEE, groundCoffee, config.getQuantityWater());\n    }\n\n    public void addBeans(CoffeeSelection selection, CoffeeBean newBeans) throws CoffeeException {\n        CoffeeBean existingBeans = this.beans.get(selection);\n        if (existingBeans != null) {\n            if (existingBeans.getName().equals(newBeans.getName())) {\n                existingBeans.setQuantity(existingBeans.getQuantity() + newBeans.getQuantity());\n            } else {\n                throw new CoffeeException(\"Only one kind of beans supported for each CoffeeSelection.\");\n            }\n        } else {\n            this.beans.put(selection, newBeans);\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Map;\n\npublic class PremiumCoffeeMachine extends BasicCoffeeMachine {\n    public PremiumCoffeeMachine(Map beans) {\n        \/\/ call constructor in superclass\n        super(beans);\n\n        \/\/ add configuration to brew espresso\n        this.configMap.put(CoffeeSelection.ESPRESSO, new Configuration(8, 28));\n    }\n\n    private Coffee brewEspresso() {\n        Configuration config = configMap.get(CoffeeSelection.ESPRESSO);\n\n        \/\/ grind the coffee beans\n        GroundCoffee groundCoffee = this.grinder.grind(this.beans.get(CoffeeSelection.ESPRESSO), config.getQuantityCoffee());\n\n        \/\/ brew an espresso\n        return this.brewingUnit.brew(\n            CoffeeSelection.ESPRESSO, groundCoffee, config.getQuantityWater());\n    }\n\n    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException {\n        if (selection == CoffeeSelection.ESPRESSO)\n            return brewEspresso();\n        else\n            return super.brewCoffee(selection);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>If you read our post about the OOP concept inheritance, you already know the two implementations of the&nbsp;<em>brewCoffee<\/em>&nbsp;method. The&nbsp;<em>BasicCoffeeMachine<\/em>&nbsp;only supports the&nbsp;<em>CoffeeSelection.FILTER_COFFEE<\/em>. The&nbsp;<em>brewCoffee&nbsp;<\/em>method of the&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;class adds support for&nbsp;<em>CoffeeSelection.ESPRESSO<\/em>.<\/p>\n\n\n\n<p>If the action gets called with any other&nbsp;<em>CoffeeSelection<\/em>, it uses the keyword&nbsp;<em>super<\/em>&nbsp;to delegate the call to the superclass.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-late-binding\"><strong>Late Binding<\/strong><\/h4>\n\n\n\n<p>Sometimes, you want to use an inheritance hierarchy in your project. To do this, you must answer the question, which method will the JVM call?<\/p>\n\n\n\n<p>The answer manifests during runtime because it depends on the object on which the method gets called. The type of the reference, which you can see in your code, is irrelevant. You need to distinguish three general scenarios:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Your object is of the type of the superclass and gets referenced as the superclass. So, in the example of this post, a&nbsp;<em>BasicCoffeeMachine<\/em>&nbsp;object gets referenced as a&nbsp;<em>BasicCoffeeMachine<\/em><\/li>\n\n\n\n<li>Your object is of the type of the subclass and gets referenced as the subclass. In the example of this post, a&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;object gets referenced as a&nbsp;<em>PremiumCoffeeMachine<\/em><\/li>\n\n\n\n<li>Your object is of the type of the subclass and gets referenced as the superclass. In the CoffeeMachine example, a&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;object gets referenced as a&nbsp;<em>BasicCoffeeMachine<\/em><\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s delve a bit further \u2026<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-superclass-referenced-as-the-superclass\"><strong>Superclass Referenced as the Superclass<\/strong><\/h5>\n\n\n\n<p>The first scenario is pretty simple. When you instantiate a&nbsp;<em>BasicCoffeeMachine<\/em>&nbsp;object and store it in a variable of type&nbsp;<em>BasicCoffeeMachine<\/em>, the JVM will call the&nbsp;<em>brewCoffee<\/em>&nbsp;method on the&nbsp;<em>BasicCoffeeMachine<\/em>&nbsp;class. So, you can only brew a&nbsp;<em>CoffeeSelection.FILTER_COFFEE<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create a Map of available coffee beans\nMap beans = new HashMap();\nbeans.put(CoffeeSelection.FILTER_COFFEE,\n\nnew CoffeeBean(\"My favorite filter coffee bean\", 1000));\n\n\/\/ instantiate a new CoffeeMachine object\nBasicCoffeeMachine coffeeMachine = new BasicCoffeeMachine(beans);\n\nCoffee coffee = coffeeMachine.brewCoffee(CoffeeSelection.FILTER_COFFEE);\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-subclass-referenced-as-the-subclass\"><strong>Subclass Referenced as the Subclass<\/strong><\/h5>\n\n\n\n<p>The second scenario is similar. But this time, we instantiate a&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;and reference it as a&nbsp;<em>PremiumCoffeeMachine<\/em>. In this case, the JVM calls the&nbsp;<em>brewCoffee<\/em>&nbsp;method of the&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;class, which adds support for&nbsp;<em>CoffeeSelection.ESPRESSO<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create a Map of available coffee beans\nMap beans = new HashMap();\nbeans.put(CoffeeSelection.FILTER_COFFEE,\n\nnew CoffeeBean(\"My favorite filter coffee bean\", 1000));\nbeans.put(CoffeeSelection.ESPRESSO,\n\nnew CoffeeBean(\"My favorite espresso bean\", 1000));\n\n\/\/ instantiate a new CoffeeMachine object\nPremiumCoffeeMachine coffeeMachine = new PremiumCoffeeMachine(beans);\n\nCoffee coffee = coffeeMachine.brewCoffee(CoffeeSelection.ESPRESSO);\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Subclass Referenced as the Superclass<\/h5>\n\n\n\n<p>This is the most interesting scenario and the main reason why we explain dynamic polymorphism in such detail.<\/p>\n\n\n\n<p>When you instantiate a&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;object and assign it to the&nbsp;<em>BasicCoffeeMachine coffeeMachine<\/em>&nbsp;variable, the object is still a&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;object. It just looks like a&nbsp;<em>BasicCoffeeMachine<\/em>.The compiler doesn\u2019t see that in the code, and you can only use the methods provided by the&nbsp;<em>BasicCoffeeMachine<\/em>&nbsp;class. If you call the&nbsp;<em>brewCoffee<\/em>&nbsp;method on the&nbsp;<em>coffeeMachine<\/em>&nbsp;variable, the JVM recognizes it as an object of the&nbsp;<em>PremiumCoffeeMachine<\/em>&nbsp;type. Then JVM executes the overridden method. This is known as late binding.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create a Map of available coffee beans\nMap beans = new HashMap();\nbeans.put(CoffeeSelection.FILTER_COFFEE,\n\nnew CoffeeBean(\"My favorite filter coffee bean\", 1000));\n\n\/\/ instantiate a new CoffeeMachine object\nBasicCoffeeMachine coffeeMachine = new PremiumCoffeeMachine(beans);\n\nCoffee coffee = coffeeMachine.brewCoffee(CoffeeSelection.ESPRESSO);\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Runtime Performance Hit for Dynamic Method Dispatch<\/h4>\n\n\n\n<p>Dynamic method dispatch relies on structures like the v-table (virtual table) in object-oriented programming languages. The term &#8220;v-table&#8221; is not explicitly mentioned in the Java Language Specification. However, most JVM implementations use a v-table or similar mechanism to achieve dynamic method dispatch.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>How Dynamic Method Dispatch Works<\/strong><\/h5>\n\n\n\n<ol class=\"wp-block-list\">\n<li>At runtime, the program determines the actual object type.<\/li>\n\n\n\n<li>A lookup is performed in the v-table (or a similar structure) to find the correct method to invoke.<\/li>\n\n\n\n<li>The method is then executed.<\/li>\n<\/ol>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Impact on Performance<\/strong><\/h5>\n\n\n\n<p>While this mechanism provides the flexibility to override methods dynamically, it comes with a performance trade-off:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>-Runtime Overhead:<\/strong> The lookup process in the v-table adds a small delay compared to direct method calls in static polymorphism.<\/li>\n\n\n\n<li><strong>-Cumulative Cost:<\/strong> In performance-critical applications (e.g., game engines or real-time systems), frequent method dispatch can lead to noticeable overhead.<\/li>\n\n\n\n<li><strong>-Cache Misses:<\/strong> The indirection introduced by v-table lookups can sometimes result in cache misses, slightly degrading performance.<\/li>\n<\/ul>\n\n\n\n<p>Despite these drawbacks, modern processors and optimized compilers mitigate much of the performance impact, making dynamic polymorphism viable for most use cases.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"200\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Polymorphism-is-one-of-the-core-concepts-in-OOP-languages-and-describes-the-concept-of-using-different-classes-with-the-same-interface.png\" alt=\"Polymorphism is one of the core concepts in OOP languages and describes the concept of using different classes with the same interface.\" class=\"wp-image-45267\" srcset=\"https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Polymorphism-is-one-of-the-core-concepts-in-OOP-languages-and-describes-the-concept-of-using-different-classes-with-the-same-interface.png 1000w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Polymorphism-is-one-of-the-core-concepts-in-OOP-languages-and-describes-the-concept-of-using-different-classes-with-the-same-interface-300x60.webp 300w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Polymorphism-is-one-of-the-core-concepts-in-OOP-languages-and-describes-the-concept-of-using-different-classes-with-the-same-interface-150x30.webp 150w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Polymorphism-is-one-of-the-core-concepts-in-OOP-languages-and-describes-the-concept-of-using-different-classes-with-the-same-interface-768x154.webp 768w, https:\/\/stackify.com\/wp-content\/uploads\/2021\/12\/Polymorphism-is-one-of-the-core-concepts-in-OOP-languages-and-describes-the-concept-of-using-different-classes-with-the-same-interface-600x120.webp 600w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary\"><strong>Summary<\/strong><\/h2>\n\n\n\n<p>Polymorphism is one of the core concepts in OOP languages and describes the concept of using different classes with the same interface. Each of these classes can provide its implementation of the interface.<\/p>\n\n\n\n<p>Java supports two kinds of polymorphism. You can overload a method with different sets of parameters. The compiler statically binds the method call to a specific method, which we know as static polymorphism.<\/p>\n\n\n\n<p>Within an inheritance hierarchy, a subclass can override a method of its superclass. The JVM will always call the overridden method if you instantiate the subclass. Even if you cast the subclass to its superclass, the result will be the same. That is dynamic polymorphism.In building robust software, you need to write flawless codes. <\/p>\n\n\n\n<p>Try Stackify\u2019s free code profiler, <a href=\"https:\/\/stackify.com\/prefix\/\">Prefix<\/a>, to write better code faster every time. Prefix now provides OpenTelementry data ingestion, which works with .NET, Java, PHP, Node.js, Ruby, Python, C++, Erlang\/Elixer, Go, Rust, and Swift.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism describes the concept that you can access objects of different types through the same interface.<\/p>\n","protected":false},"author":18,"featured_media":45264,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7],"tags":[52],"class_list":["post-14796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-developer-tips"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v25.6) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OOP Concepts for Beginners: What Is Polymorphism - Stackify<\/title>\n<meta name=\"description\" content=\"In this post, we&#039;ll discuss polymorphism, a concept in Object-Oriented Programming which allows developers to build logical code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/stackify.com\/oop-concept-polymorphism\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OOP Concepts for Beginners: What Is Polymorphism - Stackify\" \/>\n<meta property=\"og:description\" content=\"In this post, we&#039;ll discuss polymorphism, a concept in Object-Oriented Programming which allows developers to build logical code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/stackify.com\/oop-concept-polymorphism\/\" \/>\n<meta property=\"og:site_name\" content=\"Stackify\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Stackify\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-10T22:18:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"360\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Thorben Janssen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@stackify\" \/>\n<meta name=\"twitter:site\" content=\"@stackify\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thorben Janssen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/\"},\"author\":{\"name\":\"Thorben Janssen\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff\"},\"headline\":\"OOP Concepts for Beginners: What Is Polymorphism\",\"datePublished\":\"2025-02-10T22:18:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/\"},\"wordCount\":1752,\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg\",\"keywords\":[\"developer tips\"],\"articleSection\":[\"Developer Tips, Tricks &amp; Resources\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/\",\"url\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/\",\"name\":\"OOP Concepts for Beginners: What Is Polymorphism - Stackify\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg\",\"datePublished\":\"2025-02-10T22:18:38+00:00\",\"description\":\"In this post, we'll discuss polymorphism, a concept in Object-Oriented Programming which allows developers to build logical code.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/stackify.com\/oop-concept-polymorphism\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg\",\"width\":640,\"height\":360},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stackify.com\/#website\",\"url\":\"https:\/\/stackify.com\/\",\"name\":\"Stackify\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stackify.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/stackify.com\/#organization\",\"name\":\"Stackify\",\"url\":\"https:\/\/stackify.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"width\":1377,\"height\":430,\"caption\":\"Stackify\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Stackify\/\",\"https:\/\/x.com\/stackify\",\"https:\/\/www.instagram.com\/stackify\/\",\"https:\/\/www.linkedin.com\/company\/2596184\",\"https:\/\/www.youtube.com\/stackify\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff\",\"name\":\"Thorben Janssen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g\",\"caption\":\"Thorben Janssen\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"OOP Concepts for Beginners: What Is Polymorphism - Stackify","description":"In this post, we'll discuss polymorphism, a concept in Object-Oriented Programming which allows developers to build logical code.","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:\/\/stackify.com\/oop-concept-polymorphism\/","og_locale":"en_US","og_type":"article","og_title":"OOP Concepts for Beginners: What Is Polymorphism - Stackify","og_description":"In this post, we'll discuss polymorphism, a concept in Object-Oriented Programming which allows developers to build logical code.","og_url":"https:\/\/stackify.com\/oop-concept-polymorphism\/","og_site_name":"Stackify","article_publisher":"https:\/\/www.facebook.com\/Stackify\/","article_published_time":"2025-02-10T22:18:38+00:00","og_image":[{"width":640,"height":360,"url":"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg","type":"image\/jpeg"}],"author":"Thorben Janssen","twitter_card":"summary_large_image","twitter_creator":"@stackify","twitter_site":"@stackify","twitter_misc":{"Written by":"Thorben Janssen","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/#article","isPartOf":{"@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/"},"author":{"name":"Thorben Janssen","@id":"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff"},"headline":"OOP Concepts for Beginners: What Is Polymorphism","datePublished":"2025-02-10T22:18:38+00:00","mainEntityOfPage":{"@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/"},"wordCount":1752,"publisher":{"@id":"https:\/\/stackify.com\/#organization"},"image":{"@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg","keywords":["developer tips"],"articleSection":["Developer Tips, Tricks &amp; Resources"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/","url":"https:\/\/stackify.com\/oop-concept-polymorphism\/","name":"OOP Concepts for Beginners: What Is Polymorphism - Stackify","isPartOf":{"@id":"https:\/\/stackify.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage"},"image":{"@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg","datePublished":"2025-02-10T22:18:38+00:00","description":"In this post, we'll discuss polymorphism, a concept in Object-Oriented Programming which allows developers to build logical code.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/stackify.com\/oop-concept-polymorphism\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/oop-concept-polymorphism\/#primaryimage","url":"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2025\/02\/640x360-20-OOP-Concepts-for-Beginners.jpg","width":640,"height":360},{"@type":"WebSite","@id":"https:\/\/stackify.com\/#website","url":"https:\/\/stackify.com\/","name":"Stackify","description":"","publisher":{"@id":"https:\/\/stackify.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stackify.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/stackify.com\/#organization","name":"Stackify","url":"https:\/\/stackify.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/","url":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","width":1377,"height":430,"caption":"Stackify"},"image":{"@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Stackify\/","https:\/\/x.com\/stackify","https:\/\/www.instagram.com\/stackify\/","https:\/\/www.linkedin.com\/company\/2596184","https:\/\/www.youtube.com\/stackify"]},{"@type":"Person","@id":"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff","name":"Thorben Janssen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g","caption":"Thorben Janssen"}}]}},"_links":{"self":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/14796"}],"collection":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/comments?post=14796"}],"version-history":[{"count":0,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/14796\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media\/45264"}],"wp:attachment":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media?parent=14796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/categories?post=14796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/tags?post=14796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}