Professional Documents
Culture Documents
1
Reference: Liang, Y., 2015. Intro To Java Programming. 10th ed. Pearson Education Limited .
Unit Topics
1. Member methods of a class
2. Argument Types
3. Passing Object to method, returning Object from method, Array of
objects
4. Method Overloading
5. Type Conversion in Method Overloading
6. Constructor Overloading and this Keyword
2
Unit 4.1
Declaring Methods
• In order for an object A to request some service of an object B, A needs to
know the specific language with which to communicate with B. That is,
– Object A needs to be clear as to exactly which of B’s methods/services
A wants B to perform.
– Depending on the service request, object A may need to give B some
additional information so that B knows exactly how to proceed.
– Object B in turn needs to know whether object A expects B to report
back the outcome of what it has been asked to do.
3
Unit 4.1
Methods
• A method definition consists of its method name, parameters, return
value type, and body.
• Methods can be used to define reusable code and organize and simplify
code. A method is a collection of statements grouped together to perform
an operation..
• Syntax
4
Unit 4.1
Method Headers
• A method header is a formal specification (from a programming
standpoint) of how that method is to be invoked. A method header, at
minimum, consists of
• A method’s return type—that is, the type of information that is going
to be returned by object B to object A, if any, when B’s method finishes
executing.
• A method’s name.
• An optional list of comma-separated formal parameters (specifying
their types and names) to be passed to the method, enclosed in
parentheses.
5
Unit 4.1
6
Unit 4.1
Method Properties
• Can be declared in any order
• A return statement is a jump statement that is used to exit a method.
• Method implements business rule.
7
Unit 4.1
8
Unit 4.1
An Analogy
• let’s say that a person is capable of
• Taking out the trash
• Mowing the lawn
• Washing the car
• Expressing this notion in Java code, we’d perhaps declare three methods
for the Person class, one for each chore (service):
• void takeOutTheTrash()
• boolean mowTheLawn()
• void washTheCar(Car c)
9
Unit 4.1
Calling a method
• Calling a method executes the code in the method.
• To execute the method, It has to be called or invoke.
• There are two ways to call a method, depending on whether the method
returns a value or not.
1. If method returns a value;.
10
Unit 4.1
11
Unit 4.1
Parameters
• The power of a method is its ability to work with parameters.
• When calling a method, you need to provide arguments, which must be
given in the same order as their respective parameters in the method
signature. This is known as parameter order association.
• When a method is invoked with an argument, the value of the argument is
passed to the parameter.
• If the argument is a variable rather than a literal value, the value of the
variable is passed to the parameter.
12
Unit 4.1
Method Signatures
void switchMajor(String newDepartment, Professor newAdvisor)
13
Unit 4.1
Examples
• Method header: int getAge(int ageType)
• Method signature: getAge(int)
• Argument signature: (int)
• Method header:
void chooseMajor(String newDepartment, Professor newAdvisor)
• Method signature: chooseMajor(String, Professor)
• Argument signature: (String, Professor)
14
Unit 4.2
Argument Passing
• In general, there are two ways that a computer language can pass an
argument to a subroutine.
• call-by-value
This approach copies the value of an argument into the formal parameter
of the method. Therefore, changes made to the parameter of the method
have no effect on the argument.
• call-by-reference
In this approach, a reference to an argument (not the value of the
argument) is passed to the parameter. Inside the method, this reference is
used to access the actual argument specified in the call. This means that
changes made to the parameter will affect the argument used to call the
method.
15
Unit 4.2
call-by-value
Output
16
Unit 4.2
call-by-reference
Output
17
Unit 4.3
Objects as Parameters
• Just like using simple types as parameters, it is both correct and common
to pass objects to methods.
18
Unit 4.3
Objects as Parameters
• One of the most common uses of object parameters involves
constructors. Frequently, a construct is required to create a new object
that it is initially the same as some existing object.
19
Unit 4.3
Objects as Parameters
Output:
Volume of the mybox1 is 3000
Volume of the clone is 3000
20
Unit 4.3
Returning Objects
A method can return any type of data,
including class types that is created.
Output
21
Unit 4.3
Returning Objects
Output
22
Unit 4.3
Returning Objects
Output
No
23
Unit 4.3
Array of Objects
Circle[] circleArray = new Circle[10];
24
Unit 4.3
Array of Objects
… Circle object 1
25
Unit 4.3
Review Questions
1. What are the difference between method signature and method
header?
2. What is Object cloning?
3. What do you mean by returning an object from method?
4. How do define the array of object? Why array of object is useful?
26
Unit 4.4
Method Overloading
• Overloading is a language mechanism that allows two or more
different methods belonging to the same class to have the same
name as long as they have different argument signatures
27
Unit 4.4
Method Overloading
void print (String fileName)
{ ... // version #1
void print (int detailLevel)
{ ... // version #2
void print (int detailLevel, String fileName)
{ ... // version #3
int print (String reportTitle, int maxPages)
{ ... // version #4
boolean print ()
{ ... // version #5
28
Unit 4.4
Method Overloading
• When an overloaded method is invoked, Java uses the type and/or
number of arguments as its guide to determine which version of the
overloaded method to actually call.
• Overloaded methods must differ in the type and/or number of their
parameters.
• The return type alone is insufficient to distinguish two versions of a
method.
• Method overloading increases the readability of the program
29
Unit 4.4
30
Unit 4.4
31
Unit 4.5
32
Unit 4.5
33
Unit 4.6
Constructor Overloading
• In addition to this, constructors can also be overloaded.
• Constructor overloading is a technique in Java in which a class can have
any number of constructors that differ in parameter lists.
• The compiler differentiates these constructors by taking into account the
number of parameters in the list and their type.
• Constructor returns current class instance yet it does not have a return
type.
35
Unit 4.6
Error!
36
Unit 4.6
37
Unit 4.6
38
Unit 4.6
39
Unit 4.6
40
Unit 4.6
return_type method_name(){
return this;
}
41
Unit 4.6
42
Unit 4.6
43
Unit 4.6
44
Unit 4.6
Review Questions
1. Why this keyword is used?
2. Make an analogy to understand the concept of polymorphism.
3. How do you define method overloading is useful for the program
development?
45