What is a method
A method is a defined block of code within a class:
- it performs a task
- it's built inside a class
- it has a name
- it can receive values to operate on
- it can send values back to the program
- when invoked (called) it's referred to by it's name
Method Scope
The scope or visibility of a method within a class, package or program depends on the method's modifier:
public - visible to any class that uses the class that the method belongs to
private - visible only to the class it belongs to
protected - visible to all classes in the package and visible outside the package to any sub-classes that inherit it
- No modifier specified - visible to all classes across the package
Method syntax
The components of a method are:
- modifier
- return type
- name
- parameter list in parentheses
- method body enclosed between braces
The method signature, which makes a method unique, is comprised of:
An example...
Method syntax
This method's name is changeName and it's parameter type is String. It's components are -
Parameters
A parameter is a value passed into a method from elsewhere in the program.
- A method can have multiple parameters, separated by commas
- They are after the method name & surrounded by parentheses ( )
- When writing the method you must include both the data type and variable name
- When calling the method just write the variable name for each parameter
Examples...
Parameters
No parameters
One parameter
Two parameters
Different data types
Methods & data
A method interacts with data (variables). It either gets, sets, changes or just uses the data. This data is one of the followng:
- declared inside the method or
- passed into the method via its parameter list or
- declared at class level as either an instance or static variable
- See an example...
Methods & data
Methods & data
An explanation of the method and data interacting within a class:
value is a variable passed in to the method as a parameter
max is an instance variable, and max and value are compared in the method
response is a local variable, declared inside the method, that the method returns to the program. What value response holds depends on max and value.
Overloading
You can have multiple methods of the same name in a single class. Maybe they do very similar tasks. This can help make your class more understandable and make update easier.
When overloading methods:
- the signature must be different - same method name but the parameter list must be different
- if the number of parameters is the same their data types must be different
Examples...
Overloading
Here is a class, Person. The class has both an overloaded constructor and an overloaded updateAge method.
Notice that the method names remain the same but the parameter data types are different.
There is no limit to the amount of overloaded methods a class can contain - just use common sense.
Overloading
Overloading
Overriding
This can occur when inheritance is used. And it happens between a super class and it's sub classes.
- The method already exists in the super class
- The signature is the same across super and sub classes
- The method body is modified in a sub-class to suit something specific in that class
An example...
Read more in the Inheritance article
Overriding
- When calling an overridden method the Java runtime checks that the method exists in the sub class first
- If it isn't in that class, it searches up the inheritance chain until it finds the first method by that name in a super class
- This is the method it then uses
The super class
A general Cyclist class. It has a ride method, because we want the cyclist to either ride or not ride.
The sub class
A more specific Cyclist - Motorcyclist. The decision to ride is a little more complex because you can't ride a motorcycle without petrol in the tank. So the ride method is overridden and its body has some added information.
Calling the method
..load or ...ride
The diference between overloading & overriding:
- Overloading is when you define two methods with the same name, in the same class, distinguished by their signatures.
- Overloading is resolved at compile time
- Overriding is when you redefine (in a sub class) a method that has already been defined in a parent class (using the exact same signature)
- Overriding is resolved at runtime
Signatures
A method signature is the unique structure and naming of the method. The method name, the type and order of parameters all contribute to the uniqueness of it's signature.
The signature does not include the method return type.
All methods must be unique so that the compiler can tell which is which.
Examples...
Question 1
- A Sub is a method that ...
Question 2
Hint - is it a sub or function that returns a value?
Restart Quiz
Question 3
Question 4
- How many parameters can a method have?
Question 5
- A function called
calculate receives two values and subtracts the first value from the second. It then returns the resulting value to the program.
Which is the correct way to call this function?
End of Quiz
Well done. Now try some examples in Visual Studio or go to another article. ☺