Lesson 6 of 10
Methods
Methods are functions inside a class. They can accept parameters and return values. Use void when the method returns nothing. Method overloading allows multiple methods with the same name but different parameters.
JAVA
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public void greet(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(3, 4)); // 7
calc.greet("Ali");
}
}