/*  	* file : UseInt.java 
    	* demonstrate some of the mechanics of using interfaces *  
*/ 

interface Z {
  	int a = 20;
	void m();  
}  

class Y implements Z { 	

// method m must explicitly be public or compiler error 	

	public void m() {
		System.out.println("m in class Y"); 
	}  
}

public class UseInt {
  	public static void main(String[] args) {  				

		//array of interfaces is OK to declare
		// but what do we put in here ?

 		Z z[] = new Z[10];

		// it doesn't make sense to have an array of Z		
		// but it's fine to have an array of a type 		
		// that implements Z		

		for(int i = 0; i<10; i++)
 			z[i] = new Y();
 		for(int i = 0; i<10;i++)			
			System.out.println(z[i].getClass());

		// casting and converting

		Z z1;
 		Y y1 = new Y(); 		

		// no cast necessary same as z1 = (Z)y1;

		z1 = y1;
		z1.m();
		if(z1 instanceof Z)
			System.out.println("\nz1 is a Z");
		if(z1 instanceof Y)
			System.out.println("z1 is a: " +z1.getClass());

		// how about the other way ?

		Z z2 = y1;
		Y y2 = new Y();
 		
		// compiler error : explicit cast needed, can't convert
		// Z to Y even though z2 is a reference to y
		// y2 = z2;
 		// with cast		

		y2 = (Y)z2;
		y2.m();				 			
} //main				 
} 
