Whereas a C programmer might push a value onto a Stack data-structure by calling:
stackPush(&myStack, value);a C++ programmer would write:
myStack.push(value);The difference is the required level of isolation. In C, the stackPush procedure could be in the same source file as the rest of the program and if it was, any other pieces of the program in that source file could see and modify all of the low level details of how the stack was implemented, completely bypassing the intended interface. In C++, regardless of where the class is placed, only the functions which are part of myStack will be able to get acess to those low-level details without going through the formal interface functions. Languages such as C can provide comparable levels of protection by using different source files and not providing external linkage to the private parts of the stack implementation but this is less neat and systematic than the more cohesive and enforced isolation of the C++ approach.
The difference between a function and a method is that a method, being associated with a particular object, will access or modify some aspect of that object. Consequently, rather than thinking "a function is a grouped set of commands", an OO programmer will consider a method to be "this object's way of providing a service" (its "method of doing the job", hence the name); a method call should be considered to be a request to the object to perform a task. Method calls are often modelled as a means of passing a message to an object. Rather than pushing a value onto the stack, we send a value to stack, along with the message "push!", and the stack complies or raises an exception to explain why it cannot.
An instance method is a method invoked with respect to an instance of a class. Instance methods are often used to examine or modify the state of a particular object. In Java and C++, constructors are special instance methods that are called automatically upon the creation of an instance of a class; they are distinguished by having the same name as their class. In typical implementations, instance methods are passed a hidden reference to the object they belong to, so that they can access the data associated with the instance that they are called upon.
In contrast to instance methods, a class method (shared method) can be invoked without reference to a particular object. These affect an entire class, not merely a particular instance of the class. A typical example of a class method would be one that keeps count of the number of created objects within a given class. Some programming languages such as C++ and Java call them static method since methods are modified with static
.
An abstract method is a method which has no implementation. It is used to make a place-holder to be overridden later.
An accessor method is a kind of method that is usually small, simple and provides the means for the state of an object to be accessed from other parts of a program. Although it introduces a new dependency, use of the methods are preferred to directly accessing state data because they provide an abstraction layer. For example, if a bank-account class provides a "getBalance()" accessor method to retrieve the current balance (rather than directly accessing the balance data fields), then later revisions of the same code can implement a more complex mechanism balance retrieval (say, a database fetch) without the dependent code needing to be changed.
An accessor method that changes the state of an object is sometimes especially called mutator or update method. Objects with such a method are considered mutable objects.