Java Methods
Java Method Parameters
Information can be passed to methods as a parameter. Parameters act as variables inside the method.
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:
Create a Method
A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also create your own methods to perform certain actions:
Example
public class Main {
static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}
public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
Parameters and Arguments
Information can be passed to methods as a parameter. Parameters act as variables inside the method.
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:
Example
public class Main {
static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}
public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
Method Overloading & Overloading
Method overloading in Java is a feature that allows a class to have more than one method with the same name, but with different parameters. It is a way to achieve compile-time polymorphism.
In method overloading, the methods must differ in:
- Number of parameters, or
- Type of parameters, or
- Order of parameters
The return type alone is not enough to distinguish overloaded methods.
Method overloading improves code readability and reusability because the same method name can be used for similar operations with different types of input.
It is commonly used when performing similar tasks that require different kinds of data, making the program more flexible and easier to understand.
Method overloading in Java is a concept where a class can contain multiple methods with the same name but different parameter lists. It is used to perform similar operations in different ways and is an example of compile-time polymorphism.
In method overloading, methods must differ by:
- Number of parameters
- Data type of parameters
- Sequence (order) of parameters
The return type of methods does not play a role in overloading if the parameter list is the same.
This feature helps improve code readability, flexibility, and maintainability, because you can use a single method name for related operations instead of creating different names for each variation.
Example
class MathOperations {
// Method with 2 integer parameters
int add(int a, int b) {
return a + b;
}
// Method with 3 integer parameters
int add(int a, int b, int c) {
return a + b + c;
}
// Method with 2 double parameters
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperations obj = new MathOperations();
System.out.println(obj.add(5, 10)); // calls first method
System.out.println(obj.add(5, 10, 15)); // calls second method
System.out.println(obj.add(5.5, 2.5)); // calls third method
}
}
Method Overloading vs Method Overriding
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Defining multiple methods with the same name but different parameters in the same class. | Redefining a parent class method in the subclass with the same signature and compatible return type. |
| Purpose | To achieve compile-time polymorphism (static binding). | To achieve runtime polymorphism (dynamic binding). |
| Parameter List | Must be different (in number, type, or order). | Must be exactly the same as in the parent class. |
| Return Type | Can be same or different, but must not conflict. | Must be same or covariant (subtype of parent’s return type). |
| Inheritance | Not required; can occur within the same class. | Requires inheritance between superclass and subclass. |
| Access Modifier | Can have any access modifier. | Cannot reduce parent method’s access level. |
| Static / Final Methods | Can be overloaded. | Cannot be overridden if marked static or final. |
| Binding Time | Compile-time binding. | Runtime binding. |
| Exception Handling | Overloaded methods can declare any exceptions. | Overridden method cannot throw broader checked exceptions than the parent. |
| Example | void add(int a, int b) and void add(double a, double b) | Parent: void show() → Child: void show() |