In order to make use of the service provided by an object, one should know what object is capable of performing and how to request for the service. Lets say an Object A wanted to use the service of Object B, then Object A should know what service(s) Object B provides and also should talk in Object B's language inorder to get the service. This blog is about how behaviour (methods) are specified with in a class.
Lets take a realworld example of getting a service and compare it with the objects. Lets say that you are going to Railway booking counter. In order to get the service you will / should do the following
- First and foremost you should know what services are provided in railway booking counter. Examples of services are a) An option to check the ticket availability in automattic teller machine, b) To book / Cancel a Ticket c) General Enquiry etc. You cannot expect your favoirte dish to be served in Railway booking counters. Similary, to get the services from an object, another object should know what are the services provided by the object.
- You should speak the language of the ticket booking clerk. ie, if you want to book ticket, you got to fill in the application form (fill in your name, source and desingation of travel, your seat choice, train number, train name etc) and give it to the clerk. You cannot go and ask the clerk "give me a ticket" (you know what response you get in railway booking counters) . Similarily, one shouuld communicate with objects by providing information in the way object expects in-order to get its service.
- The railway ticket clerk will issue a ticket if there is a seat available, else she will say "no tickets for mentioned train". Similary, objects will perform the service and may or may not pass on the information back to the requestor.
To summarize, object should publicize their service, the requestor should communicate with the object to get its service by providing additional information if required and finally the object will peform the service and may or many not notify the requestor.
In OO terminology these three aspects of an object's behaviour are taken care by defining a method header. A method header is the formal specification of a behaviour of a object. It gives information on how to invoke the object. Its these methods that implements business logic of a system.
Note : Before reading a couple of books on OO, I used to say "calling a method". Its not the correct way to say. One should say as "invoking a method". Its not a such a big thing but for me it is..
Ok, back to method headers. A method header contains the following
- Method Return type
- Mehods accessibility modifiers (We will discuss about this future)
- Methods Name
- An optional list of formal parameters seperated by comma ( specifying the type and name of parameter) enclosed in parenthesis
public bool IsEligibleForVoting ( int iAge )
Access modifier return type MethodName Parameters
Note : Methods need not always contain paramters. In such case the parathesis are left empty
Method Signatures : Method Headers with out return type, names of parameters are called as Method signature. A method signature contains method name, order, types and number of parameters being passed to the method.
Example Method Header : long AddTwoNumbers (int a, int b)
Method Signature : AddTwoNumbers(int, int)
What are Arugments and Parametes ?
|A paramete is a locally scoped variable declared in method header, that temporarirly comes to existence when the method is executed.||Arguments are values that are passed to the method when they are invoked.|
|They are often interchanged in our programming world. Is there any big deal in the names? Yes, ofcourse. An Emigrant in one country is called as Imigrant in another coutnry, though they bother refer to the same person the level of freedom varies… so is case with parameters and arugmetns.|
The purpose of the arguments is to pass information to methods.