Method (computer science)
In computer science,method isfunction or subroutine thatassociated withclassobject-oriented programming. Likefunctionprocedural languages,may containsetprogram statements that perform an action,(in most computer languages) can takesetinput argumentscan return some kindresult.WhereasC programmer might pushvalue ontoStack data-structure by calling:
stackPush(&myStack, value);a C++ programmer would write:
myStack.push(value);The difference isrequired levelisolation. In C,stackPush procedure could be insame source file asrest ofprogramifwas, any other pieces ofprogramthat source file could seemodify all oflow level detailshowstack was implemented, completely bypassingintended interface. In C++, regardlesswhereclassplaced, onlyfunctions whichpartmyStack will be ableget acessthose low-level details without going throughformal interface functions. Languages such as C can provide comparable levelsprotection by using different source filesnot providing external linkage toprivate parts ofstack implementation but thisless neatsystematic thanmore cohesiveenforced isolation ofC++ approach.
The difference betweenfunction andmethodthatmethod, being associated withparticular object, will access or modify some aspectthat object. Consequently, rather than thinking "a function isgrouped setcommands", an OO programmer will considermethodbe "this object's wayprovidingservice" (its "methoddoingjob", hencename);method call should be consideredberequest toobjectperformtask. Method callsoften modelled asmeanspassingmessagean object. Rather than pushingvalue ontostack, we sendvaluestack, along withmessage "push!", andstack complies or raises an exceptionexplain whycannot.
An instance method ismethod invokedrespectan instance ofclass. Instance methodsoften usedexamine or modifystate ofparticular object. In JavaC++, constructorsspecial instance methods thatcalled automatically uponcreationan instance ofclass; theydistinguished by havingsame name as their class. In typical implementations, instance methodspassedhidden reference toobjectbelong to, so thatcan accessdata associated withinstance that theycalled upon.
In contrastinstance methods,class method (shared method) can be invoked without reference toparticular object. These affect an entire class, not merelyparticular instance ofclass. A typical example ofclass method would be one that keeps count ofnumbercreated objects withingiven class. Some programming languages such as C++Java call them static method since methodsmodifiedstatic.
An abstract method ismethod which has no implementation. Itusedmakeplace-holderbe overridden later.
An accessor method iskindmethod thatusually small, simpleprovidesmeans forstatean objectbe accessed from other parts ofprogram. Althoughintroducesnew dependency, use ofmethodspreferreddirectly accessing state data becauseprovide an abstraction layer. For example, ifbank-account class provides"getBalance()" accessor methodretrievecurrent balance (rather than directly accessingbalance data fields), then later revisions ofsame code can implementmore complex mechanism balance retrieval (say,database fetch) withoutdependent code needingbe changed.
An accessor method that changesstatean objectsometimes especially called mutator or update method. Objectssuch methodconsidered mutable objects.
