Method Prototype
1. What Is a Prototype? (beginner)
Prototype = method's signature line (promise!): return type + name + params, NO body. Like function declaration in C. Tells caller what to give, what comes back.
2. Anatomy (dissect one line!)
public static int add(int a, int b); // prototype (ends with ; — no body!)
// | | | | |_________|
// | | | | params (inputs with types!)
// | | | name (verb, camelCase!)
// | | return type (output kind!)
// | static (no object needed!)
// access (who can call!)
Full method = prototype + { body }. Interface/abstract use bare prototypes (body later!).
3. Code — Prototype vs Definition
class Calc {
int add(int a, int b); // ERROR in class! (needs body — prototypes only in interface/abstract!)
}
interface Adder {
int add(int a, int b); // OK! (interface = prototypes only, class implements!)
}
class MyCalc implements Adder {
public int add(int a, int b) { return a + b; } // definition (body!)
}
4. Traps
- Class method needs body:
int f();in class = ERROR (add{}or make abstract!). mainprototype fixed:public static void main(String[] args)(exam asks each word!).- Overloading = same name, different params (different prototypes!).
5. Exam Points
- Dissect prototype (5 parts) — 3-5 marks, frequent.
- Prototype vs definition + interface use — 3 marks, frequent.