Method Types in Java
A Complete Overview
Your Name | Date
Introduction
• Methods are blocks of code that perform specific tasks.
• They help in code reusability and modular programming.
• Methods in Java can be classified based on return type, parameters, access, invocation, and special beh
Based on Return Type - Void Methods
• Void Methods: Do not return a value
Example:
public void greet() {
[Link]("Hello!");
}
Based on Return Type - Return-Type Methods
• Return-Type Methods: Return a value of specified type
Example:
public int sum(int a, int b) {
return a + b;
}
Based on Parameters - No-Argument Methods
• No-Argument Methods: No input parameters
Example: void displayMessage() {}
Based on Parameters - Parameterized Methods
• Parameterized Methods: Accept input parameters
Example: void greetUser(String name) {}
Access Modifiers - Public
• Accessible from anywhere
Access Modifiers - Private
• Accessible only within the class
Access Modifiers - Protected
• Accessible within package & subclasses
Access Modifiers - Default
• Accessible only within the package
Invocation - Instance Methods
• Belong to objects
• Called using object reference
Invocation - Static Methods
• Belong to class
• Called using class name
• Declared with 'static'
Special Methods - Abstract
• Abstract Methods – no body, must override
Example:
abstract class Shape {
abstract void draw();
}
Special Methods - Final
• Final Methods – cannot be overridden
Example:
public final void display() {
[Link]("Cannot be overridden");
}
Special Methods - Synchronized
• Synchronized Methods – thread-safe
Example:
public synchronized void update() {}
Special Methods - Native
• Native Methods – implemented outside Java
Example:
public native void nativeMethod();
Summary Table - Part 1
Void Method – No return – void greet() {}
Return-Type Method – Returns value – int sum() {}
No-Arg Method – No parameters – void display() {}
Summary Table - Part 2
Parameterized Method – With parameters – void greet(String name) {}
Instance Method – Needs object – [Link]()
Static Method – Belongs to class – [Link]()
Summary Table - Part 3
Abstract Method – No body, must override – abstract void draw();
Final Method – Cannot override – final void show() {}
Synchronized Method – Thread-safe – synchronized void update() {}
Native Method – Implemented outside Java – native void nativeMethod();
Conclusion - Part 1
• Java methods organize code into reusable blocks.
Conclusion - Part 2
• Understanding method types helps in OOP design & optimization.
Conclusion - Part 3
• Use access modifiers, static/instance, and special methods as needed.