More On Methods and Classes: CS F213: Object Oriented Programming
More On Methods and Classes: CS F213: Object Oriented Programming
Dipanjan Chakraborty
Department of CS&IS
1
Method Overloading
●
OOP concept: polymorphism
●
Multiple methods in the same class can have the same name, as
long as the parameters declaration is different
– Method overloading
●
The return type may differ as well, but for overloading the
parameters must be different
●
Java uses automatic type conversion if no exact match is found
2
Method Overloading: advantages
●
A language like C has different functions to find the
absolute value of a number:
– For int: abs()
– For long: labs()
– For float: fabs()
●
A programmer needs to remember the method names and
apply the correct one for the corresponding data type
3
Method Overloading
●
Modify the Box class from last time to have a
constructor for a cube: same value for all
dimensions
●
Add another constructor which takes another
Box object as a parameter
4
Method Overloading
●
Construct a Class and overload methods to add two
numbers and two vectors
– You would need to define a vector class, and
objects can be used in overloading and as
parameters to methods
●
Add overloaded methods to test for equality of two
numbers and vectors
5
Parameter Passing
●
Java uses call by value when passing
parameters to methods
– Recall: call by value vs call by reference
●
However, the behaviour is different when a
primitive type (e.g. int, double) is passed vs
when an object is passed
6
Parameter Passing
●
When a primitive data type is passed:
– A copy is made in the called method, so changes in the copy does
not affect the original values
●
When an Object is passed, a reference to the object is passed
– So, even though a copy is made of the reference in the called
method, they refer to the same object as the calling method
– So, any changes made to one reflects in the other
●
Let’s look at an example
7
Recursion
●
Java supports recursion
●
Write a Class which uses recursion to compute
the factorial of an integer
8
Access Control
●
OOP concept: encapsulation
●
Recap: Controlling access prevents misuse
●
A class should create a ‘black box’ which may be
used but not tampered with
●
How a class member can be accessed is defined
by the access modifier attached to the declaration
9
Access Control
●
Access modifiers:
– Public: can be accessed by any other code
– Private: can be accessed only by other members of its
class
– Protected (applies only when inheritance is involved)
– Default (no access modifier specified): public within its
own package (a package is a group of classes)
10
The static keyword
●
When a class member is declared static, it can be used without
requiring an object to be first instantiated
– e.g. the main() method is static
●
Both methods and variables can be static
●
Instance variables which are static are global variables
– When objects of that class are declared, copies of static
variables are not made. All instances of the class share the
same static variable
11
Restrictions when using static methods
●
static methods can only call other static
methods of their class directly
●
static methods can access only the static
variables of their class
●
static methods can not refer to this or super (to
be discussed with inheritance)
12
The static keyword
●
Test out the ramifications of static methods
and variables through an example
●
Modify the RandomOps class to convert the
add() method to a static class
13
The final keyword
●
Local Variables, parameters to methods, methods and classes can be declared
as final
●
When local variables or method parameters are declared final, their values
can not be changed any further. They can be assigned values during
declaration or in a constructor
●
When a method is declared final, it can not be overridden (will be discussed
later)
●
When a class is declared final, it can not be inherited (will be discussed later)
●
Let’s look at a few examples
14
Arrays as Objects
●
Java arrays are implemented as objects
●
All array objects have a member variable
length which corressponds to the size of the
array
●
Modify the Stack class to make use of the
length variable of an array
15
Nested and Inner Classes
●
Classes can be defined within another class
●
Suppose Class B has been defined within class A
– Class B’s scope is bounded by the enclosing class. B
does not have an independent existence outside A
●
B has access to members of A, including private members
●
A does not have access to members of B
16
Nested and Inner Classes
●
Static nested class: static modifier has been applied
– must access the non-static members of its enclosing class
through an object
●
Non-static nested class: aka inner class
– has access to all of the variables and methods of its outer
class and may refer to them directly in the same way that
other non-static members of the outer class do
17
Strings in Java
● System.out.println(“This is a String”);
– “This is a String” is a String object in Java
●
String objects are immutable in Java. That is, once created
they can not be changed
– If change is required, create a new one
– StringBuffer and StringBuilder classes allow string
manipulation
18
Strings in Java
● String myString = “This is a test”;
●
The + operator defined for Strings is used to concatenate
strings
– String myString = “I” + “like” + “Java”
19
Methods in the String class
● boolean equals(secondString)
● int length()
● char charAt(index)
●
Let’s look at an example
20
Command-Line Arguments
● public static void main(Strings[] args)
● args is an array of Strings
●
The Strings must be converted to other types if required
(will check it out later)
21
Varargs: Variable Length Arguments
●
Older methods:
– Create overloaded methods for all possible number of arguments
– By passing an array
●
Varargs:
– static void vaTest(int ... v)
– vaTest() can be called with 0 or more arguments. Implicitly v is declared as an array of
type int[]
– Normal parameters and varargs together:
● int method(int a, int b, double c, int ... vals)
– The varargs must be the last parameter, there can be only one varargs parameter
22
Ambiguities in varargs
●
Consider these overloaded methods:
– vaTest(int ... v) {}
– vaTest(boolean ... v) {}
●
Consider this call:
– vaTest();
25