Ch3 User-Defined Methods: What Is A Method in Java?
Ch3 User-Defined Methods: What Is A Method in Java?
Why Functions/Methods?
• To cope with complexity
• Hiding details
• Reuse
Naming a Method
• Single-word method name: sum(), area()
• Multi-word method name: areaOfCircle(), stringComparision()
• It is also possible that a method has the same name as another method
name in the same class, it is known as method overloading.
1
Types of Methods
There are two types of methods in Java:
• Predefined Method
eg. Math.pow(), Math.sqrt()
• User-defined Method
2
Method Overloading
• In Java, two or more methods can have same name but if they differ in
parameters (different number of parameters, different types of
parameters, or both).
3
• Pure functions does not modify the object
public static boolean after(Time t1,Time t2)
{
boolean result;
result=(t1.hour>t2.hour)?true:false;
return result;
}