🧩 Object-Oriented Programming

User-defined Methods

⏱ 4 hr4 topicsInteractive
🎯 By the end: You can write and call methods, overload them, tell actual from formal parameters, explain pass by value vs reference, and classify methods as static/non-static and pure/impure.

A method is a named block of code that does one job. Methods let you write a task once and reuse it, breaking a big program into small, testable pieces. This is a core ICSE Class 10 topic.

1Why methods — structure, prototype & signature

Methods avoid repeating code and make programs readable. A method's general form:

returnType  name(parameter list) {
  // body
  return value;   // if returnType is not void
}

int add(int a, int b) {     // returns an int
  return a + b;
}
TermMeans
PrototypeThe method's heading: return type + name + parameter types
SignatureThe method name + its parameter list (types & order)
returnSends a value back; a void method returns nothing
Key points
  • A method is a named, reusable block of code that does one job.
  • Form: returnType name(parameters) { body }; void means it returns nothing.
  • Signature = method name + parameter list; prototype = the method's heading.

2Method overloading

Overloading means writing several methods with the same name but different parameter lists (different number or types of parameters). Java picks the right one from the arguments you pass.

int add(int a, int b)          { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c)   { return a + b + c; }

add(2, 3);        // calls the first
add(2.5, 1.0);    // calls the second
add(1, 2, 3);     // calls the third
Overloaded methods must differ in their parameter list. Changing only the return type is NOT enough to overload.
Key points
  • Overloading = same method name, different parameter lists (count or types).
  • Java chooses which overloaded method to run from the arguments passed.
  • Differing only by return type is NOT valid overloading.

3Parameters: actual vs formal; value vs reference

When you call a method you pass actual parameters (arguments); the method receives them as formal parameters:

int square(int n) {     // n is the FORMAL parameter
  return n * n;
}

square(5);              // 5 is the ACTUAL parameter

Pass by value vs pass by reference

Pass by valuePass by reference
What is passedA copy of the valueThe location of the original
Changes affect original?NoYes
In JavaPrimitives (int, double…)Objects/arrays (the reference is passed)
Real-world idea: pass by value is giving a photocopy (edits don't touch the original); pass by reference is giving the original file (edits change it).
Key points
  • Actual parameters are the values you pass; formal parameters receive them in the method.
  • Pass by value passes a copy (original unchanged) — Java does this for primitives.
  • Pass by reference passes the location (changes affect the original) — Java passes object/array references this way.

4static vs non-static; pure vs impure

static methodnon-static method
Belongs toThe class itselfAn object of the class
Called asClassName.method()object.method()
ExampleMath.sqrt(9)s.display()

Pure vs impure methods

  • A pure method does not change the object's state (member variables) — it only reads/returns. E.g. a method that returns the area from existing data.
  • An impure method changes the object's state (modifies member variables).
int getMarks() { return marks; }       // pure (only reads)
void setMarks(int m) { marks = m; }     // impure (changes state)
Key points
  • A static method belongs to the class (Math.sqrt); a non-static method belongs to an object.
  • A pure method does not change the object's state (only reads/returns).
  • An impure method changes the object's state (modifies member variables).

★ Practical: write some methods

In BlueJ, write methods that:

  1. A method int max(int a, int b) that returns the larger value.
  2. Overload it as double max(double a, double b).
  3. A pure method that returns a stored value and an impure method that changes it.
  4. Identify the actual and formal parameters in one of your calls.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.