POLYMORPHISM

POLYMORPHISM

Data can be processed in more than one form means same operation can be perform differently depending upon the data parameters polymorphism is working upon.

Poly means many and phism means forms. Polymorphism means one name having many forms. For e.g. if we there is method with name sum, it might be having many forms to take sum of different types of numbers and total number of input as sum can be of 2 numbers or n numbers

Polymorphism is the ability of an instance to behave differently in different situations.

There are two types of Polymorphism

  • Compile Time Polymorphism or Method Overloading
  • Run Time Polymorphism or Dynamic Method Dispatch or Method Over-riding


METHOD OVERLOADING

Overloading occurs if several methods have the same name but either of two conditions are matched

  • Number of parameters of those methods are different
  • Parameters have different data types

Note : return type do not participate in method overloading.

Java allows you to overload both methods and constructors.In fact, you can overload the main method also. Signature of the method is used to describe the method completely, name of the method with its parameter types is specified. The return type is not a part of method signature i.e. we cannot have two methods with same names and same parameter types but different return types.Reason is it will lead to ambiguity, the compiler will get confuse which method to call.

 

OVERLOADING RESOLUTION

The compiler makes a list of all the methods and sort them. Compiler pick the method by matching the datatypes of parameter in the signatures of the various methods with the datatype of values used in the specific method call.

If the compiler cannot match the parameters or more than one match is possible then the compiler gives compile-time-error,we call this complete process overloading resolution.


Method Overloading Example



Constructor Overloading Example



RUNTIME POLYMORPHISM OR DYNAMIC METHOD DISPATCH

 

Run-time polymorphism let us wait until program gets executed before deciding the object whose reference is to be stored. Let's understand with example.

We have Triangle , Rectangle and Square classes. Each of them have method area but we are stuck because we don't know what type of object our user will want to create until our program runs.

We can use Runtime Polymorphism, see how. We know from Inheritance that a subclass object reference can be assigned to a superclass variable.You may be wondering why Java allows this when it actually is STRONGLY TYPE CHECKER, well my friend it does this to support Runtime Polymorphism.

We can write code calling area method on various variables and will decide what type of object i.e. object of which class - Triangle , Rectangle or Square class will be stored in those variables at run-time.



 METHOD OVER-RIDING  METHOD OVER-LOADING
It occurs in different classes with inheritance  It occurs within the same class

 Two different classes in which one inherits the other.

They both contain method with same name and same signatures

 Class contain method with same name but with different signatures.
 We cannot decrease the scope of access modifier of the overridden method in the subclass.  There is no problem of scope of access modifiers.
 Binding occurs at runtime called as LATE BINDING  Binding occurs at compile time called as UGLY BINDING.


Let me ask you a question:

What will be the output of the code below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Test
 {

	public String getCountryName()
	{
		return "India" ;
	}
	
	public StringBuffer getCountryName()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("Russia");
		return sb;
	}
	
	public static void main(String[] args) {
		

		Test b = new Test();
		System.out.println(  b.getCountryName().toString() );
	}

}


Compilation Error
method getCountryName() is already defined in class Test
	public StringBuffer getCountryName()
method getCountryName() is already defined in class Test
	public String getCountryName()