What are the key reasons for using @protocols in Objective C? -
this question has reply here:
what protocol? 2 answerswhy want utilize protocol rather create subclass , inherit methods..?
please explain me, i'm confused topic, i'm not pleased explanation in book im reading.
where utilize protocols instead of other ways methods..? if can subclass class , methods why want utilize protocol need define methods?
why want utilize protocol rather create subclass , inherit methods..?
protocols create possible unrelated classes implement same interface. instances of each of classes can used client of protocol. example, uitableviewdatasource
protocol provides interface table can inquire info object implements protocol. table view doesn't care type of object long implements info source interface.
imagine how unpleasant things if table info sources had inherit mutual class! objective-c provides single inheritance, you'd constrained using single kind of object info source. protocols, though, info source can view controller, model object, or perhaps remote object.
to more specific, protocols allow form of polymorphism. means single object can take several forms: e.g. view controller, table info source, table delegate, scroll view delegate. because objective-c single-inheritance language, 1 of interfaces via inheritance. rest implement yourself, makes sense because adopt given protocol in order customize other object's behavior anyway.
objective-c objective-c-protocol
No comments:
Post a Comment