Methods and Functions in Java

Introduction
In the previous lesson, we explored control flow statements, which allow us to dictate the execution path of a program. Now, we delve into methods and functions, fundamental building blocks that enable code reusability, modularity, and better organization.
What is a Method?
In Java, a method is a block of code designed to perform a specific task. Unlike some languages, Java does not differentiate between methods and functions; all functions in Java are technically methods since they belong to classes.
Defining Methods
A method is defined using the following syntax:
returnType methodName(parameters) {
// Method body
return value; // Optional
}
In Java, not all methods return a value. Methods with a return type of void do not return any value.
Example:
public class Calculator {
public int add(int a, int b) { // Method definition
return a + b;
}
}
Types of Methods in Java
Java provides different types of methods based on their functionality:
Predefined Methods – Built-in methods from the Java libraries (e.g.,
Math.sqrt(),System.out.println()).User-Defined Methods – Methods created by the programmer to perform specific tasks.
Static Methods – Methods declared with the
statickeyword, which can be called without creating an instance of the class.Instance Methods – Methods that require an instance of the class to be called.
Abstract Methods – Methods declared in an abstract class without an implementation (used in inheritance).
Final Methods – Methods that cannot be overridden in subclasses.
Method Parameters and Return Types
Methods may accept parameters and return values.
Methods with Parameters
Parameters allow methods to accept input values.
public class Greeter {
public void greet(String name) {
System.out.printf("Hello, %s!%n", name);
}
}
Methods with Return Types
Methods may return values using the return keyword.
public class SquareCalculator {
public int square(int num) {
return num * num;
}
}
Void Methods
A method that does not return any value uses the void keyword.
public void displayMessage() {
System.out.println("This is a void method.");
}
Method Overloading
Method overloading allows multiple methods in the same class to have the same name but different parameter lists.
Example:
public class MathOperations {
public int multiply(int a, int b) {
return a * b;
}
public double multiply(double a, double b) {
return a * b;
}
}
In this case, the multiply method works for both integers and doubles, making the code more flexible.
Method Overriding
Method Overriding allows a subclass to provide a specific implementation of a method already defined in its superclass. This feature enables inheritance and polymorphism, enhancing code reusability and modularity.
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
myAnimal.makeSound(); // Output: Animal makes a sound
Animal myDog = new Dog();
myDog.makeSound(); // Output: Dog barks
}
}
Conclusion
Methods are essential for structuring Java programs efficiently. By understanding how to define, use, overload and override methods, you can write modular and reusable code, reducing redundancy and improving maintainability. Mastering methods lays a strong foundation for object-oriented programming concepts in Java.
About the author
I am a passionate software engineer in training at ALX. I have a strong preference for back-end development. I am on course to share my learning journey with you.
Comments
Join the discussionNo comments yet. Be the first to comment.




