

{"id":7390,"date":"2018-02-07T10:06:35","date_gmt":"2018-02-07T10:06:35","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=7390"},"modified":"2026-05-18T16:52:08","modified_gmt":"2026-05-18T11:22:08","slug":"polymorphism-in-java","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/","title":{"rendered":"Polymorphism in Java &#8211; Master the Concept in Just 7 Mins."},"content":{"rendered":"<p>To understand the concepts of polymorphism in Java, take a real-life example first.<\/p>\n<p>Remember we talked about a class having different objects?<\/p>\n<p>Take the example of a class Human and an object of this class named \u201cAaron\u201d. Aaron is a father and has two children. He lives with his family in Delhi and works for DataFlair. Now, imagine what Aaron is to his wife. Yes, he is her husband.<\/p>\n<p>Similarly, he is a father to his two children, a neighbor to his neighbors, and a coworker to anyone who works with him at DataFlair.<\/p>\n<p>This is polymorphism, which means multiple forms.<\/p>\n<h3>What is Polymorphism in Java?<\/h3>\n<p>Let\u2019s understand with an example, when water is kept at room temperature, it behaves as a liquid, but when it is kept in a cool region with low temperature, it acts as a solid. It means it changes its form according to the environment. The concept of Polymorphism is similar when the same methods or objects act differently under certain conditions, then it is called polymorphism.<\/p>\n<p>Polymorphism in Java is a single method having multiple functions under the same name. A single action gets executed in different ways.<\/p>\n<p><strong>In Java, polymorphism is of two types:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84910\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1.jpg\" alt=\"Types of Polymorphism in Java\" width=\"818\" height=\"402\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1.jpg 818w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1-300x147.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1-150x74.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1-768x377.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1-720x354.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1-520x256.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Polymorphism-in-Java-1-320x157.jpg 320w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/a><\/p>\n<p><strong>1.\u00a0 Runtime polymorphism<\/strong><br \/>\n<strong>2. Compile-time polymorphism<\/strong><\/p>\n<p>A Java object that can pass the \u201cIS-A\u201d test, i.e, signifies an \u201cis-a\u201d relationship, is an example of polymorphism.<\/p>\n<p>The word polymorphism consists of two parts, \u201cpoly\u201d signifying \u201cmany\u201d, and \u201cmorph\u201d, signifying \u201cforms\u201d.<\/p>\n<h3>Need for Java Polymorphism<\/h3>\n<p>Polymorphism allows the programmer to write code that is easy to understand. How?<\/p>\n<p>Let us see the example of a class Car.<\/p>\n<p>It has values like mileage and functions such as acceleration. However, a subclass, say, for example, Rolls-Royce, won&#8217;t have the default acceleration value.<\/p>\n<p>For a programmer to correctly define this function, he needs to rewrite the same function again.<\/p>\n<p>However, polymorphism allows the programmer to use the same function name and redefine it in the child class, which has a different implementation of the same method.<\/p>\n<h3>Compile-time polymorphism in Java<\/h3>\n<p>This polymorphism is a static polymorphism. Operator overloading and function overloading implement this type of polymorphism.<\/p>\n<p>In this type of polymorphism, the resolution of the method is done during compile time.<\/p>\n<p>In the case of method overloading, we observed that the compiler resolved the method during runtime. Method overriding has compile-time polymorphism.<\/p>\n<h4>Operator Overloading in Java<\/h4>\n<p>Operator overloading is the process of overloading an operator to perform different functions.<\/p>\n<p>In Java, only the plus operator is allowed to have different functions.<\/p>\n<p>Let us see an example that will strengthen our ability to understand this concept.<\/p>\n<p><strong>Program to understand the Operator overloading in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\npublic class OperatorOverload {\r\n  public static void PlusOperator(int num1, int num2) {\r\n    System.out.println(\"The plus operator can add two integers! \" + (num1 + num2));\r\n\r\n  }\r\n  public static void PlusOperator(String str1, String str2) {\r\n    System.out.println(\"The plus operator can also concatenate two strings! \" + (str1 + str2));\r\n  }\r\n  public static void main(String[] args) {\r\n    String str1 = \"Data\",\r\n    str2 = \"Flair\";\r\n    int num1 = 10,\r\n    num2 = 14;\r\n    PlusOperator(str1, str2);\r\n    PlusOperator(num1, num2);\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The plus operator can also concatenate two strings! DataFlair<br \/>\nThe plus operator can add two integers! 24<\/div>\n<h4>Method Overloading in Java<\/h4>\n<p>Method overloading is the process of having the same function name with different arguments. The compiler decides which function to call while compiling the program.<\/p>\n<p>All of the functions have the same method name, but they have different arguments, which makes them unique.<\/p>\n<p>For example, two functions, namely area, have the same name, but one returns the area of a square and the other returns the area of a rectangle.<\/p>\n<p><em><strong>Note that the square area function will have only one argument, whereas the one calculating the area of a rectangle will have two arguments.<\/strong><\/em><\/p>\n<p><strong>An example program to understand the concept of Method Overloading in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\npublic class CourseOverload {\r\n  public static int add(int num1, int num2) {\r\n    return num1 + num2;\r\n  }\r\n  public static double add(double num1, double num2) {\r\n    return num1 + num2;\r\n  }\r\n  public static void main(String[] args) {\r\n    int a = 6,\r\n    b = 90;\r\n    double c = 12.12,\r\n    d = 23.12;\r\n    System.out.println(add(a, b));\r\n    System.out.println(add(c, d));\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">96<br \/>\n35.24<\/div>\n<p>As you can see, the values are different for the same name of the function.<\/p>\n<p>The compiler determines the function to call while compiling the program.<\/p>\n<p>This is space-efficient and is easy to understand because the name of the function is the same. Debugging the program becomes easier.<\/p>\n<h3>Runtime Polymorphism in Java<\/h3>\n<p>Runtime polymorphism, as the name specifies, signifies dynamic resolving of the function.<\/p>\n<p>Simply put, this type of polymorphism gets resolved during the execution of the program. Method overriding implements this type of polymorphism.<\/p>\n<p><strong>Program to illustrate Java runtime polymorphism of multiple inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\nclass Car {\r\n  void fuel() {\r\n    System.out.println(\"Can have diesel or petrol as the fuel\");\r\n  }\r\n}\r\nclass Sedan extends Car {\r\n  void fuel() {\r\n    System.out.println(\"Shiny Car, but runs on diesel. \");\r\n  }\r\n}\r\nclass Engine extends Sedan {\r\n  void fuel() {\r\n    System.out.println(\"Converting diesel to smooth motion!\");\r\n  }\r\n  public static void main(String args[]) {\r\n    Car a1,\r\n    a2,\r\n    a3;\r\n    a1 = new Car();\r\n    a2 = new Sedan();\r\n    a3 = new Engine();\r\n    a1.fuel();\r\n    a2.fuel();\r\n    a3.fuel();\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Can have diesel or petrol as the fuel<br \/>\nShiny Car, but runs on diesel.<br \/>\nConverting diesel to smooth motion!<\/div>\n<p><em><strong>There is an important point to note that data members do not override. They stay the same.<\/strong><\/em><\/p>\n<h4>Method Overriding in Java<\/h4>\n<p>As the name signifies, method overriding is the process of overriding or redefining a method that was already defined in the parent class.<\/p>\n<p>Method overriding is a cornerstone of polymorphism in Java. It enables you to redefine the behavior of inherited methods in subclasses. This creates a more specialized implementation tailored to the subclass&#8217;s specific needs. By overriding methods, you can leverage the existing functionality of parent classes while introducing customized actions in child classes. This promotes code reusability, maintainability, and flexibility in object-oriented design.<\/p>\n<p>For example, a Mercedes car, which is a subclass of the car class, will have a different function named engine in comparison to the BMW car\u2019s engine.<\/p>\n<p><strong>Program to illustrate the use of Method Overriding in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\nclass DataFlair {\r\n  public void coursename() {\r\n    System.out.println(\"We teach a variety of courses at DataFlair!\");\r\n  }\r\n}\r\nclass Java extends DataFlair {\r\n  public void coursename() {\r\n    System.out.println(\"Java is one of the most important courses taught at DataFlair.\");\r\n  }\r\n}\r\n\r\npublic class MethodOverride {\r\n  public static void main(String[] args) {\r\n    Java course = new Java();\r\n    course.coursename();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Java is one of the most important courses taught at DataFlair.<\/div>\n<p><em><strong>Note that the definition of the course name is altered in the child class. This is method overloading. <\/strong><\/em><\/p>\n<p>However, if you want to execute the superclass method, we can do so easily by using the super keyword.<\/p>\n<p><strong>An example is shown below:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\nclass DataFlair {\r\n  public void coursename() {\r\n    System.out.println(\"We teach a variety of courses at DataFlair!\");\r\n  }\r\n}\r\nclass Java extends DataFlair {\r\n  public void coursename() {\r\n    super.coursename();\r\n    System.out.println(\"Java is one of the most important courses taught at DataFlair.\");\r\n  }\r\n}\r\n\r\npublic class MethodOverride {\r\n  public static void main(String[] args) {\r\n    Java course = new Java();\r\n\r\n    course.coursename();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">We teach a variety of courses at DataFlair!<br \/>\nJava is one of the most important courses taught at DataFlair.<\/div>\n<h3>Binding in Java<\/h3>\n<p>Binding is the process of connecting the method body to the method call.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Binding-in-Java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84911\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Binding-in-Java.jpg\" alt=\"Binding in Java\" width=\"418\" height=\"368\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Binding-in-Java.jpg 418w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Binding-in-Java-300x264.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Binding-in-Java-150x132.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Binding-in-Java-320x282.jpg 320w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/a><\/p>\n<p>There are two types of binding in Java.<\/p>\n<p><strong>1. Static Binding<\/strong><br \/>\n<strong>2. Dynamic Binding<\/strong><\/p>\n<h4>Static Binding in Java<\/h4>\n<p>When the type of variable or methods to be called is determined during compilation time, that is when the compiler works is a static binding. Private static and final methods are statically bound during compilation.<\/p>\n<p><strong>Program to understand the concept of Static Binding in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\npublic class StaticBinding {\r\n  public void staticbind() {\r\n    System.out.println(\"This is static binding\");\r\n  }\r\n  public static void main(String[] args) {\r\n    StaticBinding sb = new StaticBinding();\r\n    sb.staticbind();\r\n    System.out.println(\"Static binding occurred.\");\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is static binding<br \/>\nStatic binding occurred.<\/div>\n<h4>Dynamic Binding in Java<\/h4>\n<p>Dynamic binding in Java means the properties of the method and object to be invoked are determined at runtime, when the program is executing.<\/p>\n<p>Method Overriding is essentially Dynamic Binding because the compiler decides the type of return value during execution.<\/p>\n<p><strong>Program to understand the concept of Java Dynamic Binding:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\nclass Dynamic {\r\n  public void dynamicbind() {\r\n    System.out.println(\"Original method dynamic bind of type Dynamic. \");\r\n  }\r\n}\r\n\r\npublic class DynamicBinding extends Dynamic {\r\n  public void dynamicbind() {\r\n    System.out.println(\"This is Dynamic Binding\");\r\n  }\r\n  public static void main(String[] args) {\r\n    DynamicBinding db = new DynamicBinding();\r\n    db.dynamicbind();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is Dynamic Binding<\/div>\n<p><strong>Note that the compiler cannot determine the type of the method because it is in two places, class DynamicBinding and class Dynamic. Only during runtime, the compiler binds the method name to the call. This is Dynamic Binding.<\/strong><\/p>\n<h3>Covariant return types in Java<\/h3>\n<p>Java allows different return types for overridden methods. Simply put, if the return type of a parent class is of its own type, then the same method in its child class can have a different return type.<\/p>\n<p>However, the return type is non-primitive.<\/p>\n<p>JVM allows return type-based overloading, but Java does not. This is because the JVM uses the entire signature of the method for its resolution.<\/p>\n<p>The signature contains the return type of the method, and hence the Java compiler uses Covariant return types.<\/p>\n<p><strong>Java program to understand the concept of Covariant return types:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\nclass Parent {\r\n  Parent covar() {\r\n    return this;\r\n  }\r\n\r\n}\r\nclass Child extends Parent {\r\n  Child covar() {\r\n    return this;\r\n  }\r\n  void print() {\r\n    System.out.println(\"This is the covariant return type.\");\r\n  }\r\n}\r\n\r\npublic class CovarReturn {\r\n  public static void main(String[] args) {\r\n    new Child().covar().print();\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is the covariant return type.<\/div>\n<h3>Final Keyword in Java<\/h3>\n<p>The final keyword has a lot of functions in Java. As the name suggests, it finalizes a variable, method, or class.<\/p>\n<ul>\n<li>If you try to change the value of a final variable, it will log a compile error.<\/li>\n<li>When you finalize a method, you cannot override it anymore.<\/li>\n<li>If you finalize a class, then you cannot inherit it. However, final methods do support inheritance.<\/li>\n<li>If a variable is static and final, then you cannot initialize this variable anywhere except the static block.<\/li>\n<\/ul>\n<p><em><strong>Note that constructors are never final because they are not inherited.<\/strong><\/em><\/p>\n<p><strong>Program to understand the concept of the Java final keyword:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymoprhism;\r\npublic class FinalKeyword {\r\n  final int WHEELS;\r\n  FinalKeyword() {\r\n    WHEELS = 4;\r\n  }\r\n  void print() {\r\n    System.out.println(++WHEELS); \/\/This statement would result in an error\r\n    System.out.println(WHEELS);\r\n  }\r\n  public static void main(String[] args) {\r\n    FinalKeyword f = new FinalKeyword();\r\n    f.print();\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">FinalKeyword.java:9: error: cannot assign a value to a final variable WHEELS<br \/>\nSystem.out.println(++WHEELS);\/\/This statement would result in an error<\/div>\n<p><strong>Now we will see what happens if we try to inherit a final class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\nfinal class FinalClass {\r\n  int a;\r\n\r\n}\r\n\r\npublic class FinalKeyword extends FinalClass {\r\n  final int WHEELS;\r\n  FinalKeyword() {\r\n    WHEELS = 4;\r\n  }\r\n  void print() {\r\n    \/\/System.out.println(++WHEELS);\/\/This statement would result in an error\r\n    System.out.println(WHEELS);\r\n  }\r\n  public static void main(String[] args) {\r\n    FinalKeyword f = new FinalKeyword();\r\n    f.print();\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">FinalKeyword.java:8: error: cannot inherit from final FinalClass<br \/>\npublic class FinalKeyword extends FinalClass {<\/div>\n<p><strong>Now we will see what happens if we try to override a final method:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javapolymorphism;\r\nclass FinalClass {\r\n  int a;\r\n  public final void print() {\r\n    System.out.println(\"This is a parent-class method. \");\r\n  }\r\n\r\n}\r\n\r\npublic class FinalKeyword extends FinalClass {\r\n  final int WHEELS;\r\n  FinalKeyword() {\r\n    WHEELS = 4;\r\n  }\r\n  void print() {\r\n    \/\/System.out.println(++WHEELS);\/\/This statement would result in an error\r\n    System.out.println(WHEELS);\r\n  }\r\n  public static void main(String[] args) {\r\n    FinalKeyword f = new FinalKeyword();\r\n    f.print();\r\n  }\r\n}<\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<div class=\"code-output\">FinalKeyword.java:18: error: print() in FinalKeyword cannot override print() in FinalClass<br \/>\nvoid print()<br \/>\n^The<br \/>\noverridden method is final<br \/>\n1 error<\/div>\n<h3>Advantages of Polymorphism in Java<\/h3>\n<p>Polymorphism offers several advantages that make it a valuable concept for Java programmers. Here are some key benefits:<\/p>\n<p><strong>1. Code Reusability:<\/strong> By creating generic methods that can handle different object types, you can write code that is more reusable and adaptable. This reduces redundancy and promotes code maintainability.<\/p>\n<p><strong>2. Flexibility:<\/strong> Polymorphism allows your code to work seamlessly with various object types without requiring extensive modifications. This enhances the flexibility of your programs.<\/p>\n<p><strong>3. Dynamic Binding:<\/strong> Runtime polymorphism enables the compiler to determine the appropriate method implementation at runtime based on the actual object type. This dynamic approach leads to more efficient and adaptable code execution.<\/p>\n<h3>Disadvantages of Polymorphism in Java<\/h3>\n<ul>\n<li>It is difficult for some programmers to implement polymorphism.<\/li>\n<li>Runtime polymorphism degrades machine performance as the compiler decides the return type of the method during execution.<\/li>\n<li>The readability of the program decreases because the runtime implementation points to how much time the program is going to take to execute.<\/li>\n<\/ul>\n<h3>Characteristics of Java Polymorphism<\/h3>\n<p>Apart from the ones we talk about, there are a few characteristics that seldom meet the eye. They are:<\/p>\n<p><strong>1. Operator Overloading:<\/strong>\u00a0Remember the first example we saw in this article about operator overloading?<\/p>\n<p>You may notice that Java uses the plus(+)operator to concatenate strings and add integers. This is an example of operator overloading.<\/p>\n<p><strong>2. Polymorphic Parameters:<\/strong>\u00a0There are ways by which we can have a variable of the same name but different types.<\/p>\n<p>But these may result in a condition \u201cvariable hiding,\u201d which simply means that one of the variables\u2019 values gets hidden as both of them have the same name.<\/p>\n<p>A point to note is that local and global variables with the same name can be differentiated by this keyword.<\/p>\n<p><strong>3. Polymorphic Coercion: <\/strong>This is the work of the compiler, which implicitly converts data types to prevent type errors.<\/p>\n<p>For example, when you declare a variable like this, String s=2; the compiler converts the integer value to the string implicitly.<\/p>\n<h3>Summary<\/h3>\n<p>In this article, we came to know about a lot of new concepts in the field of programming.<\/p>\n<p>Polymorphism is an important aspect of every Object-Oriented Language. Hence, a strong concept of the same is essential to write efficient code that is easy to understand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To understand the concepts of polymorphism in Java, take a real-life example first. Remember we talked about a class having different objects? Take the example of a class Human and an object of this&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[20849,7640,20852,20851,11674,20850,20848],"class_list":["post-7390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-compile-time-polymorphism-in-java","tag-java-polymorphism","tag-java-polymorphism-tutorial","tag-polymorphism-example","tag-runtime-polymorphism-in-java","tag-runtime-polymorphism-in-java-with-example","tag-types-of-polymorphism-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Polymorphism in Java - Master the Concept in Just 7 Mins. - DataFlair<\/title>\n<meta name=\"description\" content=\"Polymorphism in Java is getting different characteristics of same instance. Learn types of Polymorphism- Compile time &amp; Run-time with example.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Polymorphism in Java - Master the Concept in Just 7 Mins. - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Polymorphism in Java is getting different characteristics of same instance. Learn types of Polymorphism- Compile time &amp; Run-time with example.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-07T10:06:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-18T11:22:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Polymorphism-in-Java-df.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Polymorphism in Java - Master the Concept in Just 7 Mins. - DataFlair","description":"Polymorphism in Java is getting different characteristics of same instance. Learn types of Polymorphism- Compile time & Run-time with example.","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:\/\/data-flair.training\/blogs\/polymorphism-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Polymorphism in Java - Master the Concept in Just 7 Mins. - DataFlair","og_description":"Polymorphism in Java is getting different characteristics of same instance. Learn types of Polymorphism- Compile time & Run-time with example.","og_url":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-07T10:06:35+00:00","article_modified_time":"2026-05-18T11:22:08+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Polymorphism-in-Java-df.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Polymorphism in Java &#8211; Master the Concept in Just 7 Mins.","datePublished":"2018-02-07T10:06:35+00:00","dateModified":"2026-05-18T11:22:08+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/"},"wordCount":1765,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Polymorphism-in-Java-df.jpg","keywords":["Compile Time Polymorphism in Java","java Polymorphism","Java Polymorphism tutorial","Polymorphism Example","runtime Polymorphism in Java","Runtime Polymorphism in Java with example","Types of Polymorphism in Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/","url":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/","name":"Polymorphism in Java - Master the Concept in Just 7 Mins. - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Polymorphism-in-Java-df.jpg","datePublished":"2018-02-07T10:06:35+00:00","dateModified":"2026-05-18T11:22:08+00:00","description":"Polymorphism in Java is getting different characteristics of same instance. Learn types of Polymorphism- Compile time & Run-time with example.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Polymorphism-in-Java-df.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Polymorphism-in-Java-df.jpg","width":1200,"height":628,"caption":"Polymorphism in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Polymorphism in Java &#8211; Master the Concept in Just 7 Mins."}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=7390"}],"version-history":[{"count":23,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7390\/revisions"}],"predecessor-version":[{"id":148359,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7390\/revisions\/148359"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84909"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=7390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=7390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=7390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}