{"id":26164,"date":"2024-04-01T11:23:43","date_gmt":"2024-04-01T05:53:43","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/"},"modified":"2025-01-06T19:11:58","modified_gmt":"2025-01-06T13:41:58","slug":"polymorphism-in-java","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/","title":{"rendered":"Polymorphism in Java with Examples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"what-is-polymorphism-in-java\"><strong>What is Polymorphism in Java?<\/strong><\/h2>\n\n\n\n<p><strong>Polymorphism in Java<\/strong>&nbsp;is the task that performs a&nbsp;single action in different ways.<\/p>\n\n\n\n<p>So, languages that do not support polymorphism are not 'Object-Oriented Languages', but 'Object-Based Languages'. Ada, for instance, is one such language. Since Java supports polymorphism, it is an <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/oops-in-java\/#gl_blog_26164\" target=\"_blank\" rel=\"noreferrer noopener\">Object-Oriented Language<\/a>.<\/p>\n\n\n\n<p>Polymorphism occurs when there is inheritance, i.e., many classes are related.<\/p>\n\n\n\n<p>Inheritance is a powerful feature in Java.<a href=\"https:\/\/www.mygreatlearning.com\/blog\/inheritance-in-java\/\" target=\"_blank\" rel=\"noreferrer noopener\"> Java Inheritance<\/a> lets one class acquire the properties and attributes of another class. Polymorphism in Java allows us to use these inherited properties to perform different tasks. Thus, allowing us to achieve the same action in many different ways.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-polymorphism\"><strong>What is Polymorphism?<\/strong><\/h2>\n\n\n\n<p>The derivation of the word Polymorphism is from two different Greek words- poly and morphs. \u201cPoly\u201d means numerous, and \u201cMorphs\u201d means forms. So, polymorphism means innumerable forms. Polymorphism, therefore, is one of the most significant features of Object-Oriented Programming.<\/p>\n\n\n\n<div class=\"topics\" style=\"background:#f6f7f8\">\n\t<div class=\"site-container\">\n\t\t<h2 class=\"section-title\" class=\"section-title\" id=\"must-learn-core-java-topics\"> Must Learn Core Java  Topics <\/h2>\t\n\t\t<div class=\"topic-wrapper\">\n\t\t\t\t\t\t<a class=\"card\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/data-structures-and-algorithms-in-java\" target=\"_blank\"> Java Algorithms Free Course <\/a>\n\t\t\t\t\t\t<a class=\"card\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/java-data-structures-for-beginners\" target=\"_blank\"> Java Data Structures Free Course <\/a>\n\t\t\t\t\t\t<a class=\"card\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/jdbc-in-java\" target=\"_blank\"> JDBC In Java Free Course <\/a>\n\t\t\t\t\t\t<a class=\"card\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/java-basic-programs\" target=\"_blank\"> Java Basic Programs Free Course <\/a>\n\t\t\t\t\t\t<a class=\"card\" href=\"https:\/\/www.mygreatlearning.com\/java\/free-courses\" target=\"_blank\"> Java Free Courses <\/a>\n\t\t\t\t\t<\/div>\n\t<\/div>\n<\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\" id=\"real-life-examples-of-polymorphism\"><strong>Real-Life Examples of Polymorphism<\/strong><\/h2>\n\n\n\n<p>An individual can have different relationships with different people. A woman can be a mother, a daughter, a sister, and a friend, all at the same time, i.e. she performs other behaviors in different situations.<\/p>\n\n\n\n<p>The human body has different organs. Every organ has a different function to perform; the heart is responsible for blood flow, the lungs for breathing, the brain for cognitive activity, and the kidneys for excretion. So we have a standard method function that performs differently depending upon the organ of the body.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"polymorphism-in-java-example\"><strong>Polymorphism in Java Example <\/strong><\/h4>\n\n\n\n<p>A superclass named \u201cShapes\u201d has a method called \u201carea()\u201d. Subclasses of \u201cShapes\u201d can be \u201cTriangle\u201d, \u201ccircle\u201d, \u201cRectangle\u201d, etc. Each subclass has its way of calculating area. Using Inheritance and Polymorphism means, the subclasses can use the \u201carea()\u201d method to find the area\u2019s formula for that shape.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Shapes {\n  public void area() {\n    System.out.println(&quot;The formula for area of &quot;);\n  }\n}\nclass Triangle extends Shapes {\n  public void area() {\n    System.out.println(&quot;Triangle is \u00bd * base * height &quot;);\n  }\n}\nclass Circle extends Shapes {\n  public void area() {\n    System.out.println(&quot;Circle is 3.14 * radius * radius &quot;);\n  }\n}\nclass Main {\n  public static void main(String&#x5B;] args) {\n    Shapes myShape = new Shapes();  \/\/ Create a Shapes object\n    Shapes myTriangle = new Triangle();  \/\/ Create a Triangle object\n    Shapes myCircle = new Circle();  \/\/ Create a Circle object\n    myShape.area();\n    myTriangle.area();\n    myShape.area();\n    myCircle.area();\n  }\n}\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>The formula for the area of the Triangle is \u00bd * base * height<br>The formula for the area of the Circle is 3.14 * radius * radius<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Shape {\n    public void draw() {\n        System.out.println(\"Drawing a shape\");\n    }\n}\n\nclass Circle extends Shape {\n    @Override\n    public void draw() {\n        System.out.println(\"Drawing a circle\");\n    }\n}\n\nclass Square extends Shape {\n    @Override\n    public void draw() {\n        System.out.println(\"Drawing a square\");\n    }\n}\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Shape s1 = new Circle();\n        Shape s2 = new Square();\n\n        s1.draw(); \/\/ Output: \"Drawing a circle\"\n        s2.draw(); \/\/ Output: \"Drawing a square\"\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, we have a base class <code>Shape<\/code> with a single method <code>draw()<\/code> that prints \"Drawing a shape\" to the console. We then create two subclasses, <code>Circle<\/code> and <code>Square<\/code>, that override the <code>draw()<\/code> method to print \"Drawing a circle\" and \"Drawing a square\" respectively.<\/p>\n\n\n\n<p>In the <code>main<\/code> method, we create two instances of the <code>Shape<\/code> class, <code>s1<\/code> and <code>s2<\/code>, which are actually instances of the <code>Circle<\/code> and <code>Square<\/code> subclasses. When we call the <code>draw()<\/code> method on these objects, the correct implementation is called based on the actual type of the object, this is run-time polymorphism. The program will output: \"Drawing a circle\" and \"Drawing a square\"<\/p>\n\n\n\n<p>In this example, the <code>draw()<\/code> method is overridden in the subclasses, and this allows for the program to determine which method to use at runtime. This is known as runtime polymorphism or dynamic polymorphism, Because at runtime the JVM determines the actual type of the object and calls the corresponding method.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/www.mygreatlearning.com\/blog\/oops-concepts-in-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">OOPs concepts in Java<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-polymorphism\"><strong>Types of Polymorphism<\/strong><\/h2>\n\n\n\n<p>You can perform Polymorphism in Java via two different methods:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Method Overloading<\/li>\n\n\n\n<li>Method Overriding<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-method-overloading-in-java\"><strong>What is Method Overloading in Java?<\/strong><\/h3>\n\n\n\n<p><strong>Method overloading<\/strong> is the process that can create multiple methods of the same name in the same class, and all the methods work in different ways. Method overloading occurs when there is more than one method of the same name in the class.  <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"example-of-method-overloading-in-java\"><strong>Example of Method Overloading in Java<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Shapes {\n  public void area() {\n    System.out.println(\"Find area \");\n  }\npublic void area(int r) {\n    System.out.println(\"Circle area = \"+3.14*r*r);\n  }\n\npublic void area(double b, double h) {\n    System.out.println(\"Triangle area=\"+0.5*b*h);\n  }\npublic void area(int l, int b) {\n    System.out.println(\"Rectangle area=\"+l*b);\n  }\n\n\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Shapes myShape = new Shapes();  \/\/ Create a Shapes object\n    \n    myShape.area();\n    myShape.area(5);\n    myShape.area(6.0,1.2);\n    myShape.area(6,2);\n    \n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Find area<br>Circle area = 78.5<br>Triangle area=3.60<br>Rectangle area=12<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-method-overriding-in-java\"><strong>What is Method Overriding in Java?<\/strong><\/h3>\n\n\n\n<p>Method overriding is the process when the subclass or a child class has the same method as declared in the parent class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-method-overriding-in-java\"><strong>Example of Method Overriding in Java <\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vehicle{  \n  \/\/defining a method  \n  void run(){System.out.println(\"Vehicle is moving\");}  \n}  \n\/\/Creating a child class  \nclass Car2 extends Vehicle{  \n  \/\/defining the same method as in the parent class  \n  void run(){System.out.println(\"car is running safely\");}  \n  \n  public static void main(String args&#91;]){  \n  Car2 obj = new Car2();\/\/creating object  \n  obj.run();\/\/calling method  \n  }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Car is running safely<\/pre>\n\n\n\n<p>Also, Polymorphism in Java can be classified into two types, i.e:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Static\/Compile-Time Polymorphism<\/li>\n\n\n\n<li>Dynamic\/Runtime Polymorphism<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-compile-time-polymorphism-in-java\"><strong>What is Compile-Time Polymorphism in Java?<\/strong><\/h3>\n\n\n\n<p><strong>Compile Time Polymorphism In<\/strong> Java&nbsp;is also known as&nbsp;<strong>Static Polymorphism.<\/strong>&nbsp;Furthermore, the call to the method is resolved at compile-time. Compile-Time polymorphism is achieved through <strong>Method Overloading<\/strong>. This type of polymorphism can also be achieved through <strong>Operator Overloading<\/strong>. However, Java does not support Operator Overloading.<\/p>\n\n\n\n<p>Method Overloading is when a class has multiple methods with the same name, but the number, types, and order of parameters and the return type of the methods are different. Java allows the user freedom to use the same name for various functions as long as it can distinguish between them by the type and number of parameters. Check out some of the important questions on run time polymorphism in <a href=\"https:\/\/www.mygreatlearning.com\/blog\/java-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">java interview questions<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-compile-time-polymorphism-in-java\"><strong>Example of Compile-Time Polymorphism in Java <\/strong><\/h3>\n\n\n\n<p><em>We will do addition in Java and understand the concept of compile time polymorphism using subtract()&nbsp;<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package staticPolymorphism; \npublic class Addition \n{ \nvoid sum(int a, int b) \n{ \nint c = a+b; \nSystem.out.println(\u201c Addition of two numbers :\u201d +c); } \nvoid sum(int a, int b, int e) \n{ \nint c = a+b+e; \nSystem.out.println(\u201c Addition of three numbers :\u201d +c); } \npublic static void main(String&#91;] args) \n{ \nAddition obj = new Addition(); \nobj.sum ( 30,90); \nobj.sum(45, 80, 22); \n} \n}\n<\/code><\/pre>\n\n\n\n<p><em><strong>The output of the program will be:&nbsp;<\/strong><\/em><\/p>\n\n\n\n<p><em>Sum of two numbers: 120&nbsp;<\/em><\/p>\n\n\n\n<p><em>Sum of three numbers: 147&nbsp;<\/em><\/p>\n\n\n\n<p><em>In this program, the sum() method overloads with two types via different parameters.&nbsp;<\/em><\/p>\n\n\n\n<p><em>This is the basic concept of compile-time polymorphism in java where we can perform various operations by using multiple methods having the same name.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-runtime-polymorphism-in-java\"><strong>What is Runtime Polymorphism in Java?<\/strong><\/h3>\n\n\n\n<p><strong>Runtime polymorphism<\/strong> in Java&nbsp;is also popularly known as&nbsp;<strong>Dynamic Binding or Dynamic Method Dispatch.<\/strong>&nbsp;In this process, the call to an overridden method is resolved dynamically at runtime rather than at compile-time. You can achieve Runtime polymorphism via <strong>Method Overriding<\/strong>.<\/p>\n\n\n\n<p>Method Overriding is done when a child or a subclass has a method with the same name, parameters, and return type as the parent or the superclass; then that function overrides the function in the superclass. In simpler terms, if the subclass provides its definition to a method already present in the superclass; then that function in the base class is said to be overridden.<\/p>\n\n\n\n<p>Also, it should be noted that runtime polymorphism can only be achieved through functions and not data members.&nbsp;<\/p>\n\n\n\n<p>Overriding is done by using a reference variable of the superclass. The method to be called is determined based on the object which is being referred to by the reference variable. This is also known as <strong>Upcasting<\/strong>.<\/p>\n\n\n\n<p>Upcasting takes place when the Parent class\u2019s reference variable refers to the object of the child class. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A{} \nclass B extends A{}  \nA a=new B(); \/\/upcasting<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"examples-of-runtime-polymorphism-in-java\"><strong>Examples of Runtime Polymorphism in Java<\/strong><\/h4>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<p>In this example, we are creating one superclass Animal and three subclasses, Herbivores, Carnivores, and Omnivores. Subclasses extend the superclass and override its eat() method. We will call the eat() method by the reference variable of&nbsp;Parent class, i.e. Animal class. As it refers to the base class object and the base class method overrides the superclass method; the base class method is invoked at runtime. As Java Virtual Machine or the JVM and not the compiler determines method invocation, it is, therefore, runtime polymorphism.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal{  \n  void eat(){\nSystem.out.println(\"Animals Eat\");\n}  \n}  \nclass herbivores extends Animal{  \n  void eat(){\nSystem.out.println(\"Herbivores Eat Plants\");\n} \n  }\nclass omnivores extends Animal{  \n  void eat(){\nSystem.out.println(\"Omnivores Eat Plants and meat\");\n} \n  }\nclass carnivores extends Animal{  \n  void eat(){\nSystem.out.println(\"Carnivores Eat meat\");\n} \n  }\nclass main{\n  public static void main(String args&#91;]){ \n    Animal A = new Animal();\n    Animal h = new herbivores(); \/\/upcasting  \n\tAnimal o = new omnivores(); \/\/upcasting  \n    Animal c = new carnivores(); \/\/upcasting  \n    A.eat();\n    h.eat();\n    o.eat();  \n    c.eat();  \n  \n  }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Animals eat<br>Herbivores Eat Plants<br>Omnivores Eat Plants and meat<br>Carnivores eat meat<\/p>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<p>In this example, we are creating one superclass Hillstations and three subclasses Manali, Mussoorie, Gulmarg. Subclasses extend the superclass and override its location() and famousfor() method. We will call the location() and famousfor() method by the Parent class\u2019, i.e. Hillstations class. As it refers to the base class object and the base class method overrides the superclass method; the base class method is invoked at runtime. Also, as Java Virtual Machine or the JVM and not the compiler determines method invocation, it is runtime polymorphism.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Hillstations{  \n  void location(){\nSystem.out.println(\"Location is:\");\n}  \nvoid famousfor(){\nSystem.out.println(\"Famous for:\");\n}  \n\n}  \nclass Manali extends Hillstations {  \n  void location(){\nSystem.out.println(\"Manali is in Himachal Pradesh\");\n}  \nvoid famousfor(){\nSystem.out.println(\"It is Famous for Hadimba Temple and adventure sports\");\n}  \n  }\nclass Mussoorie extends Hillstations {  \n  void location(){\nSystem.out.println(\"Mussoorie is in Uttarakhand\");\n}  \nvoid famousfor(){\nSystem.out.println(\"It is Famous for education institutions\");\n}  \n  }\nclass Gulmarg extends Hillstations {  \n  void location(){\nSystem.out.println(\"Gulmarg is in J&amp;K\");\n}  \nvoid famousfor(){\nSystem.out.println(\"It is Famous for skiing\");\n}  \n  }\nclass main{\n  public static void main(String args&#91;]){ \n    Hillstations A = new Hillstations();\n    Hillstations M = new Manali();\n\n    Hillstations Mu = new Mussoorie();\n\n    Hillstations G = new Gulmarg();\n\n    A.location();\nA.famousfor();\n\nM.location();\nM.famousfor();\n\nMu.location();\nMu.famousfor();\n\nG.location();\nG.famousfor();\n  }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Location is:<br>Famous for:<br>Manali is in Himachal Pradesh<br>It is Famous for Hadimba Temple and adventure sports<br>Mussoorie is in Uttarakhand<br>It is Famous for education institutions<br>Gulmarg is in J&amp;K<br>It is Famous for skiing<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-run-time-polymorphism-in-java\"><strong>Example of run-time polymorphism in java <\/strong><\/h3>\n\n\n\n<p>We will create two classes Car and Innova, Innova class will extend the car class and will override its run() method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car \n{ \nvoid run() \n{ \nSystem.out.println(\u201c running\u201d); \n} \n}\nclass innova extends Car \n{ \nvoid run(); \n{ \nSystem.out.println(\u201c running fast at 120km\u201d); \n} \npublic static void main(String args&#91;]) \n{ \nCar c = new innova(); \nc.run(); \n} \n} \n<\/code><\/pre>\n\n\n\n<p><em>The output of the following program will be;&nbsp;<\/em><\/p>\n\n\n\n<p><em>Running fast at 120 km.&nbsp;<\/em><\/p>\n\n\n\n<p>Another example for run-time polymorphism in Java <\/p>\n\n\n\n<p><em>Now, let us check if we can achieve runtime polymorphism via data members.&nbsp;<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class car \n{ \nint speedlimit = 125; \n} \nclass innova extends car \n{ \nint speedlimit = 135; \npublic static void main(String args&#91;]) \n{ \ncar obj = new innova(); \nSystem.out.println(obj.speedlimit);\n}\n<\/code><\/pre>\n\n\n\n<p><em><strong>The output of the following program will be :&nbsp;<\/strong><\/em><\/p>\n\n\n\n<p><em>125&nbsp;<\/em><\/p>\n\n\n\n<p><em>This clearly implies we can't achieve Runtime polymorphism via data members. In short, a method is overridden, not the data members.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"runtime-polymorphism-with-multilevel-inheritance\"><strong>Runtime polymorphism with multilevel inheritance<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class grandfather \n{ \nvoid swim() \n{ \nSystem.out.println(\u201c Swimming\u201d); \n} \n} \nclass father extends grandfather \n{ \nvoid swim() \n{ \nSystem.out.println(\u201c Swimming in river\u201d); \n} \n} \nclass son extends father \n{ \nvoid swim() \n{ \nSystem.out.println(\u201c Swimming in pool\u201d);\n} \npublic static void main(String args&#91;]) \n{ \ngrandfather f1,f2,f3; \nf1 =new grandfather(); \nf2 = new father(); \nf3 = new son(); \nf1.swim(); \nf2.swim(); \nf3.swim(): \n} \n} \n<\/code><\/pre>\n\n\n\n<p><em><strong>The output of the following program will be:&nbsp;<\/strong><\/em><\/p>\n\n\n\n<p><em>Swimming<\/em>, <em>Swimming in river<\/em>, <em>Swimming in pool<\/em><\/p>\n\n\n\n<p><strong>Another runtime polymorphism with multilevel inheritance example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class soundAnimal \n{ \npublic void Sound() \n{ \nSystem.out.println(\"Different sounds of animal\"); }\n} \nclass buffalo extends soundAnimal \n{ \npublic void Sound() \n{ \nSystem.out.println(\"The buffalo sound- gho,gho\"); } \n} \nclass snake extends soundAnimal \n{ \npublic void Sound() \n{ \nSystem.out.println(\"The snake sound- his,his\"); } \n} \nclass tiger extends soundAnimal\n{ \npublic void Sound() \n{ \nSystem.out.println(\"The tiger sounds- roooo, rooo\"); } \n} \npublic class Animal Main \n{ \npublic static void main(String&#91;] args) \n{ \nsoundAnimal Animal = new soundAnimal(); soundAnimal buffalo = new buffalo(); \nsoundAnimal snake = new snake(); \nsoundAnimal tiger = new tiger(); \nAnimal.Sound(); \nbuffalo.Sound();\nsnake.Sound(); \ntiger.Sound(); \n} \n} \n<\/code><\/pre>\n\n\n\n<p><em><strong>The output of the following program will be;&nbsp;<\/strong><\/em><\/p>\n\n\n\n<p><em>The buffalo sound- gho,gho&nbsp;<\/em><\/p>\n\n\n\n<p><em>The snake sound- his,his&nbsp;<\/em><\/p>\n\n\n\n<p><em>The tiger sound- roooo,roooo&nbsp;<\/em><\/p>\n\n\n\n<p>We hope you got an idea about runtime and compile-time polymorphism.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"polymorphic-subtypes\"><strong>Polymorphic Subtypes<\/strong><\/h2>\n\n\n\n<p>Subtype basically means that a subtype can serve as another type's subtype, sounds a bit complicated?&nbsp;<\/p>\n\n\n\n<p>Let's understand this with the help of an example:<\/p>\n\n\n\n<p>Assuming we have to draw some arbitrary shapes, we can introduce a class named \u2018shape\u2019 with a draw() method. By overriding draw() with other subclasses such as circle, square, rectangle, trapezium, etc we will introduce an array of type \u2018shape\u2019 whose elements store references will refer to \u2018shape\u2019 subclass references. Next time, we will call draw(), all shapes instances draw () method will be called.<\/p>\n\n\n\n<p>This Subtype polymorphism generally relies on upcasting and late binding. A casting where you cast up the inheritance hierarchy from subtype to a supertype is termed upcasting.<\/p>\n\n\n\n<p>To call non-final instance methods we use late binding. In short, a compiler should not perform any argument checks, type checks, method calls, etc, and leave everything on the runtime.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-polymorphism-in-programming\"><strong>What is Polymorphism in Programming?<\/strong><\/h2>\n\n\n\n<p>Polymorphism in programming is defined usage of a single symbol to represent multiple different types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-polymorphism-variables\"><strong>What is Polymorphism Variables?<\/strong><\/h2>\n\n\n\n<p>A <strong>polymorphic variable<\/strong> is defined as a variable that can hold values of different types during the course of execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-use-polymorphism-in-java\"><strong>Why use Polymorphism in Java?<\/strong><\/h2>\n\n\n\n<p>Polymorphism in Java makes it possible to write a method that can correctly process lots of different types of functionalities that have the same name. We can also gain consistency in our code by using polymorphism.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"advantages-of-polymorphism-in-java\"><strong>Advantages of Polymorphism in Java <\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It provides reusability to the code. The classes that are written, tested and implemented can be reused multiple times. Furthermore, it saves a lot of time for the coder. Also, the one can change the code without affecting the original code.<\/li>\n\n\n\n<li>A single variable can be used to store multiple data values. The value of a variable you inherit from the superclass into the subclass can be changed without changing that variable\u2019s value in the superclass; or any other subclasses.<\/li>\n\n\n\n<li>With lesser lines of code, it becomes easier for the programmer to debug the code.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"characteristics-of-polymorphism\"><strong>Characteristics of Polymorphism<\/strong><\/h3>\n\n\n\n<p>Polymorphism has many other characteristics other than Method Overloading and Method Overriding. They include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Coercion<\/li>\n\n\n\n<li>Internal Operator Overloading<\/li>\n\n\n\n<li>Polymorphic Variables or Parameters<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1-coercion\"><strong>1. Coercion<\/strong><\/h4>\n\n\n\n<p>Coercion deals with implicitly converting one type of object into a new object of a different kind. Also, this is done automatically to prevent type errors in the code.&nbsp;<\/p>\n\n\n\n<p>Programming languages such as <a href=\"https:\/\/www.mygreatlearning.com\/blog\/c-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\">C<\/a>, java, etc support the conversion of value from one data type to another data type. Data type conversions are of two types, i.e., implicit and explicit.&nbsp;<\/p>\n\n\n\n<p>Implicit type conversion is automatically done in the program and this type of conversion is also termed coercion.&nbsp;<\/p>\n\n\n\n<p>For example, if an operand is an integer and another one is in float, the compiler implicitly converts the integer into float value to avoid type error.<\/p>\n\n\n\n<p><strong>Example: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class coercion {\n\n  public static void main(String&#91;] args) {\n    Double area = 3.14*5*7;\nSystem.out.println(area);\nString s = \"happy\";\nint x=5;\nString word = s+x;\nSystem.out.println(word);\n\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>109.9<br>happy5<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"2-internal-operator-overloading\"><br><strong>2. Internal Operator Overloading<\/strong><\/h4>\n\n\n\n<p>In Operator Overloading, an operator or symbol behaves in more ways than one depending upon the input context or the type of operands. It is a characteristic of static polymorphism. Although Java does not support user-defined operator overloading like C++, where the user can define how an operator works for different operands, there are few instances where Java internally overloads operators.<\/p>\n\n\n\n<p>Operator overloading is the concept of using the operator as per your choice. Therefore, an operator symbol or method name can be used as a 'user-defined' type as per the requirements.&nbsp;<\/p>\n\n\n\n<p>For example, \u2018+\u2019 can be used to perform the addition of numbers (same data type) or for concatenation of two or more strings.<\/p>\n\n\n\n<p>In the case of +, can be used for addition and also for concatenation.<\/p>\n\n\n\n<p><strong>For example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class coercion {\n\n  public static void main(String&#91;] args) {\n    \nString s = \"happy\";\nString s1 = \"world\";\nint x=5;\nint y=10;\n\nSystem.out.println(s+s1);\nSystem.out.println(x+y);\n\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<p>happyworld<br>15<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>Similarly, operators like<strong>! &amp;, and |<\/strong>&nbsp;are also in the overload position for logical and bitwise operations. In both of these cases, the type of argument will decide how the operator will interpret.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"3-polymorphic-variables-or-parameters\"><strong>&nbsp;3. Polymorphic Variables or Parameters<\/strong><\/h4>\n\n\n\n<p>In Java, the object or instance variables represent the polymorphic variables. This is because any object variables of a class can have an IS-A relationship with their own classes and subclasses.<\/p>\n\n\n\n<p>The Polymorphic Variable is a variable that can hold values of different types during the time of execution.<\/p>\n\n\n\n<p>Parametric polymorphism specifies that while class declaration, a field name can associate with different types, and a method name can associate with different parameters and return types.<\/p>\n\n\n\n<p><strong>For example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Shape\n{\npublic void display()\n{\nSystem.out.println(\"A Shape.\");\n}\n}\nclass Triangle extends Shape\n{\npublic void display()\n{\nSystem.out.println(\"I am a triangle.\");\n}\n}\nclass Main{\npublic static void main(String&#91;] args)\n{\nShape obj;\nobj = new Shape();\nobj.display();\nobj = new Triangle();\nobj.display();\n}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>A Shape.<br>I am a triangle.<\/p>\n\n\n\n<p>Here, the obj object is a polymorphic variable. This is because the superclass\u2019s same object refers to the parent class (Shape) and the child class (Triangle).&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"problems-with-polymorphism\"><strong>Problems with Polymorphism&nbsp;<\/strong><\/h2>\n\n\n\n<p>With lots of advantages, there are also a few disadvantages of polymorphism.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Polymorphism is quite challenging while implementation. <\/li>\n\n\n\n<li>It tends to reduce the readability of the code. <\/li>\n\n\n\n<li>It raises some serious performance issues in real-time as well.<\/li>\n<\/ul>\n\n\n\n<p><strong>Type Identification During Downcasting&nbsp;<\/strong><br><\/p>\n\n\n\n<p>Downcasting is termed as casting to a child type or casting a common type to an individual type.<\/p>\n\n\n\n<p>So, we use downcasting whenever we need to access or understand the behaviour of the subtypes.&nbsp;<\/p>\n\n\n\n<p><strong>Example,&nbsp;<\/strong><\/p>\n\n\n\n<p>This is a hierarchical example&nbsp;<\/p>\n\n\n\n<p>Food&gt; Vegetable&gt; Ladyfinger, Tomato&nbsp;<\/p>\n\n\n\n<p>Here, tomato and ladyfinger are two subclasses.&nbsp;<\/p>\n\n\n\n<p>In downcasting, we narrow the type of objects, which means we are converting common type to individual type.&nbsp;<\/p>\n\n\n\n<p>Vegetable vegetable = new Tomato();&nbsp;<\/p>\n\n\n\n<p>Tomato castedTomato = (Tomato) vegetable;&nbsp;<\/p>\n\n\n\n<p>Here we are casting common type to an individual type, superclass to subclass which is not possible directly in java.<\/p>\n\n\n\n<p>We explicitly tell the compiler what the runtime type of the object is.<\/p>\n\n\n\n<p><strong>Fragile base class problem&nbsp;<\/strong><\/p>\n\n\n\n<p>Fragile base class problem is nothing but a fundamental architectural problem.&nbsp;<\/p>\n\n\n\n<p>Sometimes the improper design of a parent class can lead a subclass of a superclass to use superclass in some unpredicted ways.&nbsp;<\/p>\n\n\n\n<p>The fragility of inheritance will lead to broken codes even when all the criteria is met.&nbsp;<\/p>\n\n\n\n<p>This architectural problem is termed as a fragile base class problem in object-oriented programming systems and language.&nbsp;<\/p>\n\n\n\n<p>Basically, the reason for the fragile base problem is that the developer of the base class has no idea of the subclass design. There is no solution yet for this problem.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>We hope you must have got a basic idea of polymorphism in Java and how we use it as well as problems related to them.&nbsp;<\/p>\n\n\n\n<p>Hence, this brings us to the end of the blog on Polymorphism in Java. Furthermore, to learn more about programming and other related concepts, check out the courses on&nbsp;<a href=\"https:\/\/www.mygreatlearning.com\/academy\/#our-courses\" target=\"_blank\" rel=\"noreferrer noopener\">Great Learning Academy<\/a> and <a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\">PG Programs in Software Engineering<\/a>. &nbsp;<\/p>\n\n\n\n<p>Also, if you are preparing for Interviews, check out these&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/blog\/java-interview-questions\/\" target=\"_blank\">Interview Questions for Java<\/a>&nbsp;to ace it like a pro.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Java Interview Questions and Answers | Java Fundamentals For Beginners | Great Learning\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/bHrbM5cPvVw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>So, don\u2019t stop your journey of learning.&nbsp;Also, don't forget to upskill and reskill yourself. Keep exploring and keep learning.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions\"><strong>Frequently Asked Questions<\/strong><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1660044201432\"><strong class=\"schema-faq-question\">What is polymorphism with example?<\/strong> <p class=\"schema-faq-answer\">One of the\u00a0OOPs features\u00a0that allows\u00a0us to carry out a single action in various ways is known as\u00a0polymorphism in Java. For example, we have a class Animal with a method sound(). This is a generic class and so we cannot give it an implementation such as: Meow, Oink, Roar, etc.\u00a0<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1660044461292\"><strong class=\"schema-faq-question\">What are the 4 types of polymorphism?<\/strong> <p class=\"schema-faq-answer\">The four types of polymorphism are: <br\/>- Runtime or Subtype polymorphism<br\/>- Overloading or Parametric polymorphism<br\/>- Compile-time or Ad hoc polymorphism<br\/>- Casting or Coercion polymorphism<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1660044606356\"><strong class=\"schema-faq-question\">What is polymorphism in OOPs?<\/strong> <p class=\"schema-faq-answer\">One of the core concepts of OOP or object-oriented programming, polymorphism describes situations in which a particualr thing occurs in different forms. In computer science, polymorphism describes a concept that allows us to access different types of objects through the same interface.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1660044729018\"><strong class=\"schema-faq-question\">What is overriding in OOP?<\/strong> <p class=\"schema-faq-answer\">In object-oriented programming, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1660044779301\"><strong class=\"schema-faq-question\">What is overriding vs overloading?<\/strong> <p class=\"schema-faq-answer\">If two or more methods in the same class have the same name, but have different parameters, this is known as Overloading. In case of Overriding, a method signature (name and parameters) are found in the same superclass and the child class.<\/p> <\/div> <\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\"><\/div>\n\n\n\n<p>Engaging in the study of Java programming suggests a keen interest in the realm of software development. For those embarking upon this journey with aspirations towards a career in this field, it is recommended to explore the following pages in order to acquire a comprehensive understanding of the development career path. To further enhance your learning, explore <strong><a href=\"https:\/\/www.mygreatlearning.com\/academy\" target=\"_blank\" rel=\"noreferrer noopener\">online courses with certificates<\/a><\/strong> that will help you gain valuable skills and credentials in Java programming and software development.<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"has-cyan-bluish-gray-background-color has-background\"><tbody><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/certificates\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses certificates<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/placements\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses placements<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/syllabus\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses syllabus<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/fees\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses fees<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/eligibility\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses eligibility<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>What is Polymorphism in Java? Polymorphism in Java&nbsp;is the task that performs a&nbsp;single action in different ways. So, languages that do not support polymorphism are not 'Object-Oriented Languages', but 'Object-Based Languages'. Ada, for instance, is one such language. Since Java supports polymorphism, it is an Object-Oriented Language. Polymorphism occurs when there is inheritance, i.e., many [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":26266,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36826],"content_type":[36248],"class_list":["post-26164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-java","content_type-career-guide"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Polymorphism in Java with Examples - Great Learning<\/title>\n<meta name=\"description\" content=\"Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action in different ways.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/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 with Examples\" \/>\n<meta property=\"og:description\" content=\"Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action in different ways.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-01T05:53:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T13:41:58+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Polymorphism in Java with Examples\",\"datePublished\":\"2024-04-01T05:53:43+00:00\",\"dateModified\":\"2025-01-06T13:41:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/\"},\"wordCount\":2866,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1098956327.jpg\",\"keywords\":[\"java\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/\",\"name\":\"Polymorphism in Java with Examples - Great Learning\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1098956327.jpg\",\"datePublished\":\"2024-04-01T05:53:43+00:00\",\"dateModified\":\"2025-01-06T13:41:58+00:00\",\"description\":\"Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action in different ways.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044201432\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044461292\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044606356\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044729018\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044779301\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1098956327.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1098956327.jpg\",\"width\":1000,\"height\":750,\"caption\":\"Polymorphism in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Polymorphism in Java with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044201432\",\"position\":1,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044201432\",\"name\":\"What is polymorphism with example?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"One of the\u00a0OOPs features\u00a0that allows\u00a0us to carry out a single action in various ways is known as\u00a0polymorphism in Java. For example, we have a class Animal with a method sound(). This is a generic class and so we cannot give it an implementation such as: Meow, Oink, Roar, etc.\u00a0\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044461292\",\"position\":2,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044461292\",\"name\":\"What are the 4 types of polymorphism?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The four types of polymorphism are: u003cbr\\\/u003e- Runtime or Subtype polymorphismu003cbr\\\/u003e- Overloading or Parametric polymorphismu003cbr\\\/u003e- Compile-time or Ad hoc polymorphismu003cbr\\\/u003e- Casting or Coercion polymorphism\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044606356\",\"position\":3,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044606356\",\"name\":\"What is polymorphism in OOPs?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"One of the core concepts of OOP or object-oriented programming, polymorphism describes situations in which a particualr thing occurs in different forms. In computer science, polymorphism describes a concept that allows us to access different types of objects through the same interface.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044729018\",\"position\":4,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044729018\",\"name\":\"What is overriding in OOP?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"In object-oriented programming, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044779301\",\"position\":5,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/polymorphism-in-java\\\/#faq-question-1660044779301\",\"name\":\"What is overriding vs overloading?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"If two or more methods in the same class have the same name, but have different parameters, this is known as Overloading. In case of Overriding, a method signature (name and parameters) are found in the same superclass and the child class.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Polymorphism in Java with Examples - Great Learning","description":"Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action in different ways.","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:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Polymorphism in Java with Examples","og_description":"Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action in different ways.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2024-04-01T05:53:43+00:00","article_modified_time":"2025-01-06T13:41:58+00:00","og_image":[{"width":1000,"height":750,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Polymorphism in Java with Examples","datePublished":"2024-04-01T05:53:43+00:00","dateModified":"2025-01-06T13:41:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/"},"wordCount":2866,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg","keywords":["java"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/","url":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/","name":"Polymorphism in Java with Examples - Great Learning","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg","datePublished":"2024-04-01T05:53:43+00:00","dateModified":"2025-01-06T13:41:58+00:00","description":"Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action in different ways.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044201432"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044461292"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044606356"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044729018"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044779301"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg","width":1000,"height":750,"caption":"Polymorphism in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Polymorphism in Java with Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044201432","position":1,"url":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044201432","name":"What is polymorphism with example?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"One of the\u00a0OOPs features\u00a0that allows\u00a0us to carry out a single action in various ways is known as\u00a0polymorphism in Java. For example, we have a class Animal with a method sound(). This is a generic class and so we cannot give it an implementation such as: Meow, Oink, Roar, etc.\u00a0","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044461292","position":2,"url":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044461292","name":"What are the 4 types of polymorphism?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"The four types of polymorphism are: u003cbr\/u003e- Runtime or Subtype polymorphismu003cbr\/u003e- Overloading or Parametric polymorphismu003cbr\/u003e- Compile-time or Ad hoc polymorphismu003cbr\/u003e- Casting or Coercion polymorphism","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044606356","position":3,"url":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044606356","name":"What is polymorphism in OOPs?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"One of the core concepts of OOP or object-oriented programming, polymorphism describes situations in which a particualr thing occurs in different forms. In computer science, polymorphism describes a concept that allows us to access different types of objects through the same interface.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044729018","position":4,"url":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044729018","name":"What is overriding in OOP?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"In object-oriented programming, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044779301","position":5,"url":"https:\/\/www.mygreatlearning.com\/blog\/polymorphism-in-java\/#faq-question-1660044779301","name":"What is overriding vs overloading?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"If two or more methods in the same class have the same name, but have different parameters, this is known as Overloading. In case of Overriding, a method signature (name and parameters) are found in the same superclass and the child class.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg",1000,750,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327-300x225.jpg",300,225,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327-768x576.jpg",768,576,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg",1000,750,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg",1000,750,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg",1000,750,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg",640,480,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg",96,72,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1098956327.jpg",150,113,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":1,"uagb_excerpt":"What is Polymorphism in Java? Polymorphism in Java&nbsp;is the task that performs a&nbsp;single action in different ways. So, languages that do not support polymorphism are not 'Object-Oriented Languages', but 'Object-Based Languages'. Ada, for instance, is one such language. Since Java supports polymorphism, it is an Object-Oriented Language. Polymorphism occurs when there is inheritance, i.e., many&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/26164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=26164"}],"version-history":[{"count":104,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/26164\/revisions"}],"predecessor-version":[{"id":107542,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/26164\/revisions\/107542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/26266"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=26164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=26164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=26164"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=26164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}