In: Computer Science
2.Discuss the two major benefits of using an interface and when would you use an abstract class in preference to an interface? Explain using examples
Answer:
Interface iis ian iindispensable imechanism iof iObject iOriented iprogramming. i iThe ireason iis ithat iinterface iallows iyou idescribe ithe icommunication icontract ibetween iobjects, iwhile iwith iclasses iyou idescribe inot ionly ithe icontract ibut ialso ia ibehavior. iSo iinterfaces icome iinto iplay ias isoon ias iyou ihave iobjects iwith ithe isame imethods iwhile ithey ihave inothing icommon iin itheir ibehavior iand ithis isituation iis imuch imost icommon irather ithan isituation iwhen iyou ihave ito iextend isome iclass.
One iof ithe ibenifits iof iusing iinterface iis ithat, ihere iimplementation idetail ican ibe ihidden ifrom ithe iuser, iless iconfusions iand ieasy iswapping ibetween iimplementations.
It iis ieasier ito imaintain ibackward icompatibility iand ikeeps ithe ilegacy iinterface iworking iwhile ibuilding inew iones.
iAbstract iclasses imay iprovide imembers ithat ihave ialready ibeen iimplemented. iTherefore, iyou ican iensure ia icertain iamount iof iidentical ifunctionality iwith ian iabstract iclass, ibut icannot iwith ian iinterface.
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================