java - What is the use of createing an Interface array assign to instantiated object -
here have created object of b , assigned reference variable of interface.what means actually?. purpose of interface used implementation classes.what can interface when create array. means actually?.
interface i1{} class implements i1{} class b implements i1{} class c extends b {} class d{} public class lab1 { public static void main(string[] args) { i1 i1[] = new b[4]; i1[0] = new a(); i1[1] = new c(); i1[2] = new b(); //i1[3] = new d(); } }
it allows perform same operation on instances in array (by calling methods of interface).
for example :
for (i1 inst : i1) { i1.dosomething (); // dosomething() method declared interface i1 }
to make less abstract, here's more concrete example - if interface called shape
, has draw()
method, can draw shapes writing :
for (shape shape : shapesarray) { shape.draw (); }
this code doesn't care if actual instances stored in array circles, rectangles or polygons, long of them implement shape
interface.
Comments
Post a Comment