language agnostic - What does it mean to "program to an interface"? -


i have seen mentioned few times , not clear on means. when , why this?

i know interfaces do, fact not clear on makes me think missing out on using them correctly.

is if do:

iinterface classref = new objectwhatever() 

you use class implements iinterface? when need that? thing can think of if have method , unsure of object passed expect implementing iinterface. cannot think how need that... (also, how write method takes in object implements interface? possible?)

sorry if missed point.

there wonderful answers on here questions sorts of great detail interfaces , loosely coupling code, inversion of control , on. there heady discussions, i'd take opportunity break things down bit understanding why interface useful.

when first started getting exposed interfaces, confused relevance. didn't understand why needed them. if we're using language java or c#, have inheritance , viewed interfaces weaker form of inheritance , thought, "why bother?" in sense right, can think of interfaces sort of weak form of inheritance, beyond understood use language construct thinking of them means of classifying common traits or behaviors exhibited potentially many non-related classes of objects.

for example -- have sim game , have following classes:

 class housefly inherits insect {    void flyaroundyourhead(){}    void landonthings(){}  }   class telemarketer inherits person {    void callduringdinner(){}    void continuetalkingwhenyousayno(){}  } 

clearly, these 2 objects have nothing in common in terms of direct inheritance. but, both annoying.

let's our game needs have sort of random thing annoys game player when eat dinner. housefly or telemarketer or both -- how allow both single function? , how ask each different type of object "do annoying thing" in same way?

the key realize both telemarketer , housefly share common loosely interpreted behavior though nothing alike in terms of modeling them. so, let's make interface both can implement:

 interface ipest {     void beannoying();  }   class housefly inherits insect implements ipest {    void flyaroundyourhead(){}    void landonthings(){}     void beannoying() {      flyaroundyourhead();      landonthings();    }  }   class telemarketer inherits person implements ipest {    void callduringdinner(){}    void continuetalkingwhenyousayno(){}     void beannoying() {       callduringdinner();       continuetalkingwhenyousayno();    }  } 

we have 2 classes can each annoying in own way. , not need derive same base class , share common inherent characteristics -- need satisfy contract of ipest -- contract simple. have beannoying. in regard, can model following:

 class diningroom {     diningroom(person[] diningpeople, ipest[] pests) { ... }     void servedinner() {      when diningpeople eating,         foreach pest in pests          pest.beannoying();    }  } 

here have dining room accepts number of diners , number of pests -- note use of interface. means in our little world, member of pests array telemarketer object or housefly object.

the servedinner method called when dinner served , our people in dining room supposed eat. in our little game, that's when our pests work -- each pest instructed annoying way of ipest interface. in way, can have both telemarketers , houseflys annoying in each of own ways -- care have in diningroom object pest, don't care , have nothing in common other.

this contrived pseudo-code example (that dragged on lot longer anticipated) meant illustrate kind of thing turned light on me in terms of when might use interface. apologize in advance silliness of example, hope helps in understanding. and, sure, other posted answers you've received here cover gamut of use of interfaces today in design patterns , development methodologies.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -