Why we use interface as main concept in Java? -
this question has answer here:
- why use interface? standardization? 13 answers
- why implementing interfaces ? 8 answers
can please provide me best use of interface, because further complete reading have not found proves why use interface.
i have read interface not found solution why interface used. please explain me examples.
the principle of interface simple: contract.
an interface documented; methods of interface 1 thing , well, , implementations ensure contract fulfilled.
for instance, let take simple interface, iterator
. have collections.emptyiterator()
fulfills interface. whatever class x, can declare:
final iterator<x> iterator = collections.emptyiterator();
you guaranteed particular implementation correctly implement next()
, .hasnext()
, .remove()
methods.
similarly, if have list<x>
, do:
final iterator<x> iterator = list.iterator();
you guaranteed same.
accessing through interfaces allow tell users contract should use particular method of yours. implementation does not matter, , key here.
Comments
Post a Comment