Methods in Java
1. What is a Method?
Amethodis ablock of codethat performs aspecifictaskand runsonly when it is called.
👉 Methods help incode reuse, readability, and modularity.
2. Why Methods Are Used
● Avoid code repetition
● Divide program into small parts
● Easy debugging & maintenance
● Improves readability
● Supports reusability
3. Method Syntax in Java
accessModifier returnType methodName(parameters) {
// method body
return value;
// optional
}
Example:
public int add(int a, int b) {
return a + b;
}
4. Parts of a Method
Part Explanation
Access modifier Visibility of method
Return type alue returned (
V voidif
none)
Method name Identifier
Parameters Input values
Method body Code to execute
return Sends value back
5. Types of Methods in Java
(A) Predefined (Built-in) Methods
Provided by Java API.
Examples:
[Link]()
[Link]()
[Link]()
(B) User-Defined Methods
Created by programmer.
void greet() {
[Link]("Hello");
}
6. Based on Return Type
(A) Method with Return Value
int square(int x) {
return x * x;
}
(B) Method without Return Value (
void
)
void display() {
[Link]("Java");
}
7. Based on Parameters
(A) Method with Parameters
int sum(int a, int b) {
return a + b;
}
(B) Method without Parameters
void show() {
[Link]("No parameters");
}
8. Calling a Method
(A) Calling Non-Static Method
MyClass obj = new MyClass();
[Link](5, 3);
(B) Calling Static Method
[Link](5, 3);
9. Static Methods
● Belong to class, not object
● Called using class name
static void hello() {
[Link]("Hello");
}
Common Static Methods
[Link]()
[Link]()
[Link]()
10. Method Overloading
Same method name, different parameters
Rules:
● Different number or type of parameters
● Return type alone is NOT enough
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
✔ Compile-time polymorphism
11. Method Overriding
Child class provides its own version of parent method
Rules:
● Same name
● Same parameters
● IS-A relationship (inheritance)
class Parent {
void show() {
[Link]("Parent");
}
}
class Child extends Parent {
void show() {
[Link]("Child");
}
}
✔ Run-time polymorphism
12. Access Modifiers in Methods
Modifier Scope
public
Everywhere
protect P
ackage +
ed
subclass
default Same package
private Same class
13. Final Methods
● Cannot be overridden
final void display() {
[Link]("Fixed");
}
14. Abstract Methods
● No body
● Must be implemented by subclass
abstract class Shape {
abstract void draw();
}
15. Passing Parameters to Methods
Call by Value (Java uses this)
void change(int x) {
x = 10;
}
✔ Original value unchanged
Passing Object Reference
void change(Student s) {
[Link] = 90;
}
✔ Object data changes
16. Varargs Method (
...
)
Allows variable number of arguments.
int sum(int... nums) {
int total = 0;
for(int n : nums)
total += n;
return total;
}
17. Recursion (Method Calling Itself)
int fact(int n) {
if(n == 1)
return 1;
return n * fact(n - 1);
}
18. Method vs Constructor
Method Constructor
Has return type No return type
Called explicitly Called automatically
Any name ame as class
S
name
19. Real-Life Examples
● Login validation method
● Calculation method
● File reading method
● Database access method
20. Complete Example
class Calculator {
static int add(int a, int b) {
return a + b;
}
static void greet() {
[Link]("Welcome");
}
public static void main(String[] args) {
greet();
[Link](add(5, 3));
}
}
21. Exam-Ready Short Answers
: Can a method return multiple values?
Q
❌
No (use object/array)
: Can we overload main method?
Q
✔ Yes
: Can we override static method?
Q
❌
No
: Can abstract class have non-abstract methods?
Q
✔ Yes