Structural Polymorphism

To understand the above points → We simulate this using Java Class Hierarchy.

  1. Polymorphic reverse

    public void reverse(Object[] objarr){
    	Object tempobj;
    	int len = objarr.length;
    
    	for(int i=0;i < n/2; i++){
    		tempobj = objarr[i];
    		objarr[i] = objarr[(n-1)-i];
    		objarr[(n-1)-i] = tempobj;
    	}
    }
    
  2. Polymorphic find

    public int find(Object[] objarr, Object o){
    	int i;
    	for(i=0;i<objarr.length;i++){
    		if(objarr[i] == o) { return i; }
    	}
    	return (-1);
    }
    
  3. Polymorphic sort

    public interface Comparable{
    	public abstract int cmp(Comparable s);
    }
    .....
    public class SortFunctions{
    	public static void quicksort(Comparable[] a){
    		...
    		//Use the code for quicksort.
    		//In place of comparing a[i] and a[j]
    		//Use a[i].cmp(a[j])
    	}
    }
    

Type Consistency

Date[] datearr = new Date[10];
Employee[] emparr = new Employee[10];

arraycopy(datearr, emparr); //Causes Runtime Error due to 
														//Incompatible type of objects used for copying!.

Problem at hand