Lecture From Chapter 6 & 7 111/06/16 1 Method of Classes
Dec 23, 2015
Lecture From Chapter 6 & 7
112/04/19 1
Method of Classes
Review
• Overloading Methods• Using objects as parameters• Argument passing• Returning objects• Recursion• Access control• Understanding static• Nested and inner classes• String class
112/04/19 2
Overloading Methods• Overloading means more
than two methods with the same class that share the same name.• This method is called
method overloading.• This is one of the ways that
Java implements polymorphism.
112/04/19 3
Class overload {void test() {//no argument}void test(int a) {//one argument}void test(int a, int b) {// two arguments
}
Example of four methods
112/04/19 4
Overloading Constructors• Constructor is to initialize the values
112/04/19 5
Example of three constructors
112/04/19 6
Using Objects as parameters• We could also pass
objects to methods
112/04/19 7
Example
112/04/19 8
Argument passing
• In java, there are two ways of passing arguments, call-by-value and call-by-reference• Call-by-value will pass the value to the method or
subroutine. It will not modify the original data.• Call-by-reference will pass the object to the
method or subroutine. It might modify the original data depending on the method.
112/04/19 9
Call-by-value, there is no change in a and b in the lecture65{}, but change in test{}
112/04/19 10
Call by object
112/04/19 11
Returning the objects• A method can return any type of data.• The return type can be class types or values
112/04/19 12
Example of returning an object, tmp
112/04/19 13
Recursion
• Recursion is the process of defining something in terms of itself.• It is a method to call itself.• For example,
1, 1, 2, 3, 5, 8, 13 etc.Here, 2 = 1 + 1 n(2) = n(1) + n(0) 3 = 2 + 1 n(3) = n(2) + n(1) 5 = 3 + 2 n(0) =1, n(1) =1
112/04/19 14
Example of recursion
112/04/19 15
Recursion - factorial
Fact(n) means n*(n -1)*(n – 2)*(n-3)…3*2*1
For example, n = 3, fact(3) = 3*2*1 = 6 n = 4, fact(4) = 4*3*2*1 = 24
The relationship isfact(1) = 1fact(n) = n*fact(n-1)
112/04/19 16
Example of recursion, page 169
112/04/19 17
Access control
• We can specify which part of program can be accessed. This will prevent misuse. • There are three access specifiers that are used in
Java. They are public, private and protected.• public: that member can be accessed by any other
code• private: member can only be accessed by other
members of its class
112/04/19 18
Example – difference between public and private
112/04/19 19
Explanation
• As a is defined as default, we can access it.• b is also defined as public, we can also access it.• c is defined as private and we cannot access it, we
have to access it through a method.
112/04/19 20
Understand static
• Sometimes, we want to define a class member that will be used independently of any object of that class. We used the definition of static (initial value).• When a member is declared static, it can be
accessed before any objects of its class are created. • main() is declared as static. • We can also declare a static block which gets
executed first (before main()).
112/04/19 21
Example
112/04/19 22
Explanation
• While loading the program, this program will set i = 1 and j = 2, • it executes the static block to compute the value of
i (4 = 3 + 1) and j (6 = 2 + 4).• It then executes main() and call String show() to
display the value.
112/04/19 23
Example – without static
112/04/19 24
Example – more
112/04/19 25
Nested Classes• We can define a class within
another class.• This class is called nested class.• The scope of a nested class is
bounded by the enclosing class.• If class B is defined within class
A, then B is known to A, but not outside A.
112/04/19 26
A
B
More about nested class
• There are two types of nested classes: static and non-static.• A static nested class is one which has the static
modifier applied.• Because it is static, it must access the members of
its enclosing class through an object.• The most important type of nested class is the
inner class.
112/04/19 27
Example of inner class
112/04/19 28
Explanation
• The program will execute static void main()….• It then create a new object called outer().• Outer() then defines the value outer_val and call
inner class.• The inner class is to display the value of outer_val.
112/04/19 29
Example of generating error
112/04/19 30
String class
• String is the most commonly used class in Java’s class library.• Every string that we created is an object of type
String.• System.out.println(“I am MSc student.”);• “I am a MSc student” is a String. It can be rewritten
as:• String name =“ I am MSc student.”• System.out.println(name);
112/04/19 31
Example
112/04/19 32
Summary
• Overloading Methods – two or more methods with the same class• Using objects as parameters – pass object• Argument passing – value and object• Returning objects – return any data type• Recursion – calling itself• Access control – public, private and protected• Understanding static- initial value• Nested and inner classes – a class within a class• String class – each string is an object
112/04/19 33