Java Reference
In-Depth Information
Methods in object oriented programming
In object oriented programming methods are used in many ways. The main purpose for using
methods is off course to provide a wrapper for your code. But they are also special cases in
which they are used in object oriented programming. In some cases they are used for meth-
od overloading as well to define a method with the same name as the class (constructor) and
method overriding. Let us see some examples.
First let us first see how methods are declared in Java.
<Modifier> <data type> method name (<data type>parameter 1, <data type>parameter 2..)
{do something
}
Let us see the parts of a method definition:
Modifiers : Modifiers define access level of the method. For example if the modifier is
defined as “public” then the method is accessible to even classes outside. If the modifier is
“private” then the method is accessible only inside the class in which it is defined.
Return type : Return type of a method declares in what data type the method will return the
result of the computation done (e.g. integer, string etc.). If the method is not returning any
values then in most programming languages the key word “void” is used in the placeholder
for data type.
Method name : Method name is the name of the method by which it is known inside or out-
side the class. you can use the method name to call it anywhere in your code.
Parameter list : parameter list is the list of variables (parameters) which are passed to the
method from some other method. Each parameter has a data type.
Exception list : during computation if any error arises then the error can be trapped using ex-
ception handling. For doing this an exception list is declared and later if they arise then can
be handled inside the method body.
Method body : this is where all computation code is written.
Search WWH ::




Custom Search