Saturday, 25 March 2017

Polymorphism-1

In General polymorphism stands for "Existing in more than one form".
In java, polymorphism is divided into two types.
1.compile time polymorphism
2.Run time polymorphism

Compile time polymorphism:

It is also called as static polymorphism.Here binding of methods to its definition takes place at compile time.We implement compile time polymorphism with method overloading concept.

What is method overloading?

Method overloading is a feature that enables us to have a class with two or more methods having same name and differ in parameters resides in the same class.

Example

import java.io.*; 
 class Titans
{
 void method_a(int a,int b)
 {
  int c;
  c=a+b;
  System.out.println("The method with two arguments is exected and result is"+c); 
 }
 void method_a(int a,int b,int c)
 {
  int d;
  d=a+b+c;
  System.out.println("The method with three arguments is executed and result is"+d);
 }
 public static void main(String []args)
 {
  Titans t=new Titans();
  t.method_a(2,3);
  t.method_a(2,3,4);
 }
}

Output :

The method with two arguments is executed and result is 5
The method with three arguments is executed and result is 9

What is the need of method overloading ?

Consider the following scenario .we want to compute the area of each Geometrical fig like square,rectangle,circle,triangle.we need methods.lets see the following cases.
case 1

  • area_of_triangle();
  • area_of_square();
  • area_of_rectangle();
  • area_of_circle();
case 2
  • area(int b,int h);
  • area(int s);
  • area(int l,int b);
  • area(int r);

conclusion:


In these cases,we are using methods for finding the area of geometrical figures.Here in case 1 we need to remember a method for finding the area of each geometrical fig.
In case 2 we used a single method called area() which calculates the area of the geometrical fig.Thus,we need not remember many methods that work conceptually same We can group different methods that conceptually perform same task with a single name.Thereby,we can avoid remembering more method names.

Chance of Ambiguity:

Ambiguity arises when there is no difference in the parameters.i.e.,methods will be having same name and same parameters. If  JVM encounters such situation,there exist ambiguity in what to invoke.
Example:
int method_a(int,int)
int method_a(int,int)
Result:compile time error.

Other side of the coin:

We have seen,what happens when two methods are identical,now we are going to discuss on what happens when there is no method that matches with the calling parameters.
    
import java.io.*; 
 class Titans
{
 void method_a(float a,float b)
 {
  System.out.println("The ints are implicitly promoted to floats and results "+a+"+"+b+" as "+(a+b));
 }
 void methode_a(char c)
 {
  System.out.println("inside a 2"+c);
 }
 public static void main(String []args)
 {
  Titans t=new Titans();
  t.method_a(2,5);
 }
}

Output:

The ints are implicitly promoted to floats and results 2.0+5.0 as 7.0
Here we don't have any method that contain float data type in it parameter list,but the int will be promoted to float ie., one type will be promoted into another implicitly.

Can we Overload main() method

yes! we can overload main method,however the overloaded main() method must be called from inside the normal main() method.This is because the execution of java program starts from the normal main() method.

Example

import java.io.*; 
 class Titans extends Code
 { 
 public static void main(String args[])
 { 
  System.out.println("This is in normal main() method");
  main(10,20);
 }
 public static void main(int a,int b)
 {
  System.out.println("The addition of "+a+"and"+b+" results "+(a+b));
  System.out.println("This computation is performed in overloaded main() method");
 } 
}

Output:

This is in normal main() method
The addition of 10and20 results 30
This computation is performed in overloaded main() method

Note:

The methods can have same name but there must be difference in their parameters.

parameter list can be differ in:

  • Number of parameters.
  • Type of parameters.
  • Order of parameters.

Can we Overload Constructor?

Yes! we can overload constructors.The appropriate constructor is invoked based on the number,type and order of parameters.
import java.io.*;
class Addition
{ 
 int res;
 double res1;
 Addition(int a,int b)
 {
  res=a+b;
 }
 Addition(double c,double d,int e)
 { 
  res1=c+d+e;
 }
 void show()
 { 
 System.out.println("Addition of two ints results "+res);
 }
 void show1()
 { 
 System.out.println("Addition of two floats and one int "+res1);
 }
}
class MainClass
{ 
 public static void main(String args[])
 { 
  Addition a1=new Addition(5,3);
  Addition a2=new Addition(2.4,5.2,2);
  a1.show();
  a2.show1();
 }
}

Output:

Addition of two ints results 8
Addition of two floats and one int 9.6

No comments:

Post a Comment