User-Defined Methods
User-Defined Methods
Modularisation – process of dividing longer and complex programs into a number of smaller units
Method/procedure/function/module/subroutine/sub-program
Types of Methods –
System-defined Methods: predefined in java, cannot be modified, e.g. – sqrt() , round() of Math class
Specify the scope of access or accessibility of a method (or a class, variable, constructor)
Outside the class, outside the package, from a sub class (inheritance)
‘protected’ –
Outside the class, outside the package but form a sub class
default –
Do not define the scope of access of methods but give special properties to them
Return Type:
Parameter List: a list of comma-separated variables enclosed within the parentheses of a method
Method Prototype or Header: the entire 1st line of the method definition
Method Signature: The method name along with the parameter list in a method prototype
Parameters which appear in the method call or method reference ( generally in the main() method )
*Any change made in the Formal Parameters is not reflected in the Actual Parameters
*Any change made in the Formal Parameters is reflected in the Actual Parameters
1. Pure Methods –
*On returning a value to their caller method, they do not change the original state of an object
2. Impure Methods –
*On returning a value to their caller method, they may change the original state of an object
*Can access only static variables and not instance variable(non-static variables)
*Can be invoked without the use of an instance, can be called by class name also and even without
that
2. Non-Static Method –
*May occupy more memory and memory allocation happens when it is called and it is deallocated
later
The concept of using the same method name multiple times with different sets of parameters
* Implements polymorphism
If a method’s signature matches that of a prior method, that method is considered a redeclaration
If the return type of the two methods differs but signatures are similar => syntax error
Method Overloading => only if – Method Name is same and –
Or
Or
Both
Even if a method lacks ‘main’ method but has static method, the static method can be executed
smoothly