Java Polymorphism Assignment

Java – Polymorphism

Polymorphism is derived from the two words one is Poly and another is Morphs. Poly means many and morphs mean form.  Polymorphism means many forms. Polymorphism is method which is capable of an object to use in many forms. It is the feature of object oriented programming. Let’s take a real time example for understanding the concept of polymorphism, when you are in school you behave like a student, when you are at home you behave like a son, when you go for the shopping you will behave like a customer. We can say one single person behaves differently in different method.

 

Polymorphism is of two types:

  1. Compile time polymorphism

  2. Runtime polymorphism

Compile time polymorphism is also known as static binding or method overloading.

Runtime polymorphism is also known as dynamic binding or method overriding.

Compile time polymorphism (static binding or method overloading):  method overloading is used to allow s a class to have two or more methods having same name only when they have different argument.

It is of three types:

  1. Different number of parameters in argument
  2. Difference in data type of arguments
  3. Sequence of data type of arguments

 

  1. Different number of parameters  in arguments: method name is same but the number of arguments are different

Syntax:

public void method_name( int a )

{

}

public void method_name( int a, char b )

{

}

 

 

 

 

Example:

class poly1

{

    public void method_name( int a )

{

    System.out.println( a );

}

public void method_name( int a, char b )

{

    System.out.println( a + “ “ + b);

}

}

Class samp

{

    Public static void main (string args[] )

    {

        poly1 obj = new poly1();

        obj.method_name( 5 );

        obj.method_name( 5 , ‘a’ );

    }

}

Output:

5

5 a

 

  1. Difference in data type of arguments:

 

Syntax:

public void method_name( int a )

{

}

public void method_name(char a )

{

}

 

Example:

 

class poly1

{

    public void method_name( int a )

{

    System.out.println( a );

}

public void method_name(char a )

{

    System.out.println( a);

}

}

Class samp

{

    Public static void main (string args[] )

    {

        poly1 obj = new poly1();

        obj.method_name( 5 );

        obj.method_name( ‘a’ );

    }

}

Output:

5

a

 

 

  1. Sequence of data type of arguments:

 

Syntax:

public void method_name( int a, char b )

{

}

public void method_name(char b, int a )

{

}

 

Example:

 

class poly1

{

    public void method_name( int a, char b )

{

    System.out.println( “hey” );

}

public void method_name(char b, int a )

{

    System.out.println( “bye” );

}

}

Class samp

{

    Public static void main (string args[] )

    {

        poly1 obj = new poly1();

        obj.method_name( 5, ‘a’ );

        obj.method_name( ‘a’ , 5);

    }

}

Output:

hey

bye

 

 

Run time polymorphism (dynamic binding or method overriding): it simply means to implement the inheritance in your program

Let’s understand the method overriding taking simple example. Suppose take a class of name human being and it has two methods of name girls and boys. They are from the same class but having the different behavior.

Example:

class cal

{

int z;  

public void add (int x, int y)

{

z = x + y;

             System.out.println (" The sum of the given numbers:"+ z  ) ;

}

public void sub(int x, int y)

{

            z = x - y;

             System.out.println (" The difference between the given numbers:" + z );

      }   

}

 

public class Calculation extends cal

{

     public void mul (int x, int y)

{

            z = x * y;

            System.out.println("The product of the given numbers:"+z);

     }

   

  public static void main(String args[])

{

         int a = 10, b = 5;

Calculation d = new Calculation();

         d.add(a, b);

         d.sub (a, b);

         d.mul(a, b);

  }

}

Output:

The sum of the given numbers: 15

The difference between the given numbers: 5

The product of the given numbers: 50

Advantages of Dynamic binding:

  1. It takes the less execution time.
  2. More performance
  3. And less memory space is taken
  4. It allocates the memory space at the time of running and after using the program it will again free the memory the space.

 

 

Always use the runtime polymorphism because it is more effective and more over java does not support the static polymorphism because it has the limitations.

 

Contact us for help in JAVA.

 

 

 

 

Share this post