🔧 Methods & Scanner (User Input)

Methods & Scanner (User Input): A method is a reusable package of code that takes input (parameters), does a job, and returns output (return) — like pressing a button on a coffee

A method is a reusable package of code that takes input (parameters), does a job, and returns output (return) — like pressing a button on a coffee machine: you give it water and beans (parameters), the machine always performs the same operation and returns coffee to you; you do not rebuild the machine's internals each time. But could we not just copy-paste the same code wherever it is needed — why do we need a method? Because when you find a bug in copied code you must fix it separately in 20 places and you will forget one; with a method you fix a single place and every call updates automatically (the DRY principle). In Java methods can be overloaded (same name, different parameters), and thanks to static typing a wrong argument type is caught at compile time; in Python this only surfaces at runtime. For a QA engineer methods are the foundation of test maintenance: if you write a shared helper like `login(user, pass)`, then when the login flow changes you edit only that method instead of updating steps in 50 tests one by one — which fundamentally prevents maintenance-driven broken tests.

How a Method Call Flows

The notes split public int myFirstMethod() into visible parts; this shows the runtime call flow.

Method Definition and Calling

Micro Lab: Code practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

What is method overloading?

Inheriting a method from another class

Defining multiple methods with the same name but different parameters

Overriding a method with @Override

Declaring a static method

Overloading: same class, same name, different parameters (count, type, order). Different from overriding (redefining an inherited method in a subclass).

In Java, what occurs when you define multiple methods with the same name but different parameter lists within the same class?

Method encapsulation