java - what is the advantage of interface over abstract classes? -


abstract class have both concrete , abstract function . interface have abstract functions.

overriding possible in both. it

what real time advantage of 1 on other ?

interfaces when want "i don't care how it, here's need done."

abstract classes when want "i know should do, , know how should in some/many of cases."

abstract classes have serious drawbacks. example:

class house {  }  class boat {  }  class houseboat extends /* uh oh!! */ {     // don't me started on farmer's insurance "autoboathome" helicopter } 

you can through via interface:

interface liveable {  }  interface floatable {  }  class houseboat implements liveable, floatable {  } 

now, abstract classes useful. example, consider abstractcollection class. defines default behavior common methods collections, isempty() , contains(object). can override these behaviors if want to, but... behavior determining if collection empty change? typically it's going size == 0.

and since won't change often, worth developer's time implement method every... single... time... every method in "solved" category? not mention when need make change it, you're going have code duplication , missed bugs if had re-implement everywhere.


Comments