Question

In: Computer Science

What do you understand by polymorphism? Why are they useful? Explain each type of polymorphism using...

  1. What do you understand by polymorphism? Why are they useful? Explain each type of polymorphism using a clear example. [15 points]

Solutions

Expert Solution

Polymorphism is the ability to print output in one or more forms.it performs single action in different ways.

It will be useful in application where one interface multiple usage.

Polymorphism is divided into two types:

Compile type polymorphism and run time polymorphism

Compile type polymorphism:it also known static polymorphism,

This type of polymorphism is used in function overloading and operator overloading.

Example: void function (int a)

Void function (int a, int b)

Void function (int c)

When there are multiple functions with different parameters

    • Example: By using different types of arguments

      // Java program for Method overloading

      class MultiplyFun {

          // Method with 2 parameter

          static int Multiply(int a, int b)

          {

              return a * b;

          }

          // Method with the same name but 2 double parameter

          static double Multiply(double a, double b)

          {

              return a * b;

          }

      }

      class Main {

          public static void main(String[] args)

          {

              System.out.println(MultiplyFun.Multiply(2, 4));

              System.out.println(MultiplyFun.Multiply(5.5, 6.3));

          }

      }

      Output:
      8
      34.65
      
    • Example: By using different numbers of arguments

      // Java program for Method overloading

      class MultiplyFun {

          // Method with 2 parameter

          static int Multiply(int a, int b)

          {

              return a * b;

          }

          // Method with the same name but 3 parameter

          static int Multiply(int a, int b, int c)

          {

              return a * b * c;

          }

      }

      class Main {

          public static void main(String[] args)

          {

              System.out.println(MultiplyFun.Multiply(2, 4));

      }

      }

          • Operator Overloading: Java also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.

            In java, Only “+” operator can be overloaded:

            • To add integers
            • To concatenate strings

            Example:

            // Java program for Operator overloading

            class OperatorOVERDDN {

                void operator(String str1, String str2)

                {

                    String s = str1 + str2;

                    System.out.println("Concatinated String - "

                                       + s);

                }

                void operator(int a, int b)

                {

                    int c = a + b;

                    System.out.println("Sum = " + c);

                }

            }

            class Main {

                public static void main(String[] args)

                {

                    OperatorOVERDDN obj = new OperatorOVERDDN();

                    obj.operator(2, 3);

                    obj.operator("joe", "now");

                }

            }

            Output:
            Sum = 5
            Concatinated String - joenow
            
        • Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
          • Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

            Example:

            // Java program for Method overridding

            class Parent {

                void Print()

                {

                    System.out.println("parent class");

                }

            }

            class subclass1 extends Parent {

                void Print()

                {

                    System.out.println("subclass1");

                }

            }

            class subclass2 extends Parent {

                void Print()

                {

                    System.out.println("subclass2");

                }

            }

            class TestPolymorphism3 {

                public static void main(String[] args)

                {

                    Parent a;

                    a = new subclass1();

                    a.Print();

                    a = new subclass2();

                    a.Print();


Related Solutions

Based on what you know about object oriented programming, inheritance, and polymorphism, why do you think...
Based on what you know about object oriented programming, inheritance, and polymorphism, why do you think it is not possible to write code like this in Java: public class X extends Y, Z { // ... } ...but can write code like this: public class A implements B, C { //... }
what is elasticity of dwmand and what do you understand by that .explain on your own...
what is elasticity of dwmand and what do you understand by that .explain on your own words
What do you understand by chromatography and explain the different types of chromatography
What do you understand by chromatography and explain the different types of chromatography
a.)    What do you understand by the term system automation? b.)    Why do you think that automation is...
a.)    What do you understand by the term system automation? b.)    Why do you think that automation is important in our day to day activities – discuss c.)     There is the fear that automation will replace human activities resulting in unemployment. Do you align with this assertion? Support you claim with relevant points d.)    What is the difference between robotics and automation?
(a) what is a Z score and why is it useful (b) When do you need...
(a) what is a Z score and why is it useful (b) When do you need to calculate and use a Z - Score (c) what are the assumptions when you use a Z score. (d) Give real-life examples where you think a Z score would be useful.
Do you believe hedge funds serve a useful purpose? Why/Why not? What are some of their...
Do you believe hedge funds serve a useful purpose? Why/Why not? What are some of their pros and cons?
What do you understand by intelligence?
What do you understand by intelligence? what are the types of intelligence.
What do you understand by learning.
What do you understand by learning. State the features of learning.
What do you understand by emotions.
What do you mean by emotions. Explain the characteristic and types of emotions.
What do you understand by learning.
What do you understand by learning. Describe it's laws and characteristics.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT