Lecture 06 - A Closer Look at Methods and Classes - Part 1
Lecture 06 - A Closer Look at Methods and Classes - Part 1
• Overloading Methods
• Overloading Constructors
• Using Objects as Parameters
• A Closer Look at Argument Passing
• Returning Objects
• Recursion
• Introducing Access Control
Overloading Methods
Overloading Methods
• In Java, it is possible to define two or more methods within the same class that share
the same name, as long as their parameter declarations are different.
• When this is the case, the methods are said to be overloaded, and the process is
referred to as method overloading.
• Method overloading is one of the ways that Java supports polymorphism.
• 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 (1) data type and/or (2) number of their
parameters.
• While overloaded methods may have different return types, the return type alone is
insufficient to distinguish two versions of a method.
Overloading Methods: Example
OUTPUT
Type Conversion in Overloading Methods
• When an overloaded method is called, Java looks for a match between the arguments
used to call the method and the method’s parameters.
• However, this match need not always be exact.
• In some cases, Java’s automatic type conversions can play a role in overload
resolution.
Example
OUTPUT
OUTPUT
Using Objects as Parameters
Using Objects as Parameters
• Similar to basic data types, objects can be passed to methods as parameters.
OUTPUT
OUTPUT
A Closer Look at Argument Passing
A Closer Look at Argument Passing
• There are two ways of argument passing:
1. Call-by-value
2. Call-by-reference
Call-by-value
• Copies the value of an argument into the formal parameter of the method.
• So, changes made to the parameter of the method have no effect on the argument.
Call-by-reference
• 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.
• So, changes made to the parameter will affect the argument used to call the method.
Call-by-value: Example
OUTPUT
Call-by-reference: Example
• When we pass
an object
reference to a
method, the
parameter that
receives it will
refer to the same
object as that
referred to by
the argument.
• Changes to the
object inside the
method do affect
the object used
OUTPUT as an argument.
Returning Objects
Returning Objects
• A method can return any type of data, including class types that we create.
• Example, in the following program, the incrByTen() method returns an object in
which the value of a is ten greater than it is in the invoking object.
OUTPUT
Returning Objects
• Each time incrByTen() is invoked, a new object is created, and a reference to it
is returned to the calling routine.
• Another point: Since all objects are dynamically allocated using new, we don’t need
to worry about an object going out of scope because the method in which it was
created terminates.
• The object will continue to exist as long as there is a reference to it somewhere in
your program.
• When there are no references to it, the object will be reclaimed the next time
garbage collection takes place.
Recursion
Recursion
• In OOP, encapsulation links data with the code that manipulates it.
• Encapsulation also provides another important attribute: Access
Control.
• Through encapsulation, we can control what parts of a program can
access the members of a class.
• By controlling access, we can prevent misuse.
• For example, allowing access to data only through a well-defined set of
methods, we can prevent the misuse of that data.
• Thus, when correctly implemented, a class creates a “black box” which
may be used, but the inner workings of which are not open to tampering.
Introducing Access Control: Stack Example
Introducing Access Control: Stack Example
• In the Stack class, while it is true that the methods push() and
pop() do provide a controlled interface to the stack, this interface is not
enforced.
• That is, it is possible for another part of the program to bypass these
methods and access the stack directly.
• Of course, in the wrong hands, this could lead to trouble.
Access Modifiers