0% found this document useful (0 votes)
1 views5 pages

02 Java Methods

Java methods are blocks of code that perform specific tasks, allowing for code reuse and better organization within classes. They can be predefined or user-defined, with rules for naming and advantages such as reusability and modularity. Method overloading and overriding are key features that enhance flexibility and polymorphism in Java.

Uploaded by

ug2302070
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

02 Java Methods

Java methods are blocks of code that perform specific tasks, allowing for code reuse and better organization within classes. They can be predefined or user-defined, with rules for naming and advantages such as reusability and modularity. Method overloading and overriding are key features that enhance flexibility and polymorphism in Java.

Uploaded by

ug2302070
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Method

Java Methods (GFG)


Java Methods are blocks of code that perform a specific task. A method allows us
to reuse code, improving both efficiency and organization. All methods in
Java must belong to a class. Methods are similar to functions and expose the
behavior of objects.
Example: Java program to demonstrate how to create and use a method.

// Creating a method
public class Geeks
{
public void printMessage() {
System.out.println("Hello, Geeks!");
}
public static void main(String[] args) {
// Create an instance of the Method class
Geeks obj = new Geeks();

// Calling the method


obj.printMessage();
}
}

Output
Hello, Geeks!

Explanation:
 printMessage() is a simple method that prints a message.
 It has no parameters and does not return anything.
 Here, first we create a method which prints Hello, Geeks!
Java Method

Syntax of a Method
<access_modifier> <return_type> <method_name>( list_of_parameters)
{
//body
}

Key Components of a Method Declaration


 Modifier: It specifies the method's access level (e.g., public, private,
protected, or default).
 Return Type: The type of value returned, or void if no value is returned.
 Method Name: It follows Java naming conventions; it should start with a
lowercase verb and use camel case for multiple words.
 Parameters: A list of input values (optional). Empty parentheses are used if
no parameters are needed.
 Exception List: The exceptions the method might throw (optional).
 Method Body: It contains the logic to be executed (optional in the case of
abstract methods).
Java Method

Types of Methods in Java


1. Predefined Method
Predefined methods are the method that is already defined in the Java class
libraries. It is also known as the standard library method or built-in method. For
example, random() method which is present in the Math class and we can call it
using the ClassName.methodName() as shown in the below example.
Example:
Math.random() // returns random value
Math.PI() // return pi value

2. User-defined Method
The method written by the user or programmer is known as a user-defined
method. These methods are modified according to the requirement.
Example:
Java Method

sayHello // user define method created above in the article


Greet()
setName()

Rules to Name a Method:


 While defining a method, remember that the method name must be
a verb and start with a lowercase letter.
 If the method name has more than two words, the first name must be a
verb followed by an adjective or noun.
 In the multi-word method name, the first letter of each word must be
in uppercase except the first word. For example, findSum, computeMax,
setX, and getX.

Advantages of Methods
 Reusability: Methods allow us to write code once and use it many times.
 Abstraction: Methods allow us to abstract away complex logic and provide
a simple interface for others to use.
 Encapsulation: Allow to encapsulate complex logic and data
 Modularity: Methods allow us to break up your code into smaller, more
manageable units, improving the modularity of your code.
 Customization: Easy to customize for specific tasks.
 Improved performance: By organizing your code into well-structured
methods, you can improve performance by reducing the amount of code.
Java Method

Method Overloading
Method overloading is a feature in Java that allows multiple methods with the
same name but different parameter lists to exist within the same class. It
enhances the flexibility and readability of the code.
1. public class PrintUtil {
2. // Method with one parameter
3. public void print(String message) {
4. System.out.println(message);
5. }
6. // Overloaded method with two parameters
7. public void print(String message, int times) {
8. for (int i = 0; i < times; i++) {
9. System.out.println(message);
10. }
11. }
12. }

Method Overriding
Method overriding occurs when a subclass provides a specific implementation for
a method that is already defined in its superclass. It is a key aspect of
polymorphism in Java, allowing subclasses to customize or extend the behavior of
inherited methods.
1. class Animal {
2. // Method to be overridden
3. public void sound() {
4. System.out.println("Animal makes a sound");
5. }
6. }
7. class Dog extends Animal {
8. // Overriding method
9. @Override
10. public void sound() {
11. System.out.println("Dog barks");
12. }
13. }

You might also like