![Page 1: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/1.jpg)
Lecture From Chapter 6 & 7
112/04/19 1
Method of Classes
![Page 2: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/2.jpg)
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
![Page 3: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/3.jpg)
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
}
![Page 4: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/4.jpg)
Example of four methods
112/04/19 4
![Page 5: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/5.jpg)
Overloading Constructors• Constructor is to initialize the values
112/04/19 5
![Page 6: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/6.jpg)
Example of three constructors
112/04/19 6
![Page 7: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/7.jpg)
Using Objects as parameters• We could also pass
objects to methods
112/04/19 7
![Page 8: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/8.jpg)
Example
112/04/19 8
![Page 9: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/9.jpg)
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
![Page 10: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/10.jpg)
Call-by-value, there is no change in a and b in the lecture65{}, but change in test{}
112/04/19 10
![Page 11: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/11.jpg)
Call by object
112/04/19 11
![Page 12: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/12.jpg)
Returning the objects• A method can return any type of data.• The return type can be class types or values
112/04/19 12
![Page 13: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/13.jpg)
Example of returning an object, tmp
112/04/19 13
![Page 14: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/14.jpg)
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
![Page 15: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/15.jpg)
Example of recursion
112/04/19 15
![Page 16: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/16.jpg)
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
![Page 17: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/17.jpg)
Example of recursion, page 169
112/04/19 17
![Page 18: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/18.jpg)
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
![Page 19: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/19.jpg)
Example – difference between public and private
112/04/19 19
![Page 20: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/20.jpg)
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
![Page 21: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/21.jpg)
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
![Page 22: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/22.jpg)
Example
112/04/19 22
![Page 23: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/23.jpg)
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
![Page 24: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/24.jpg)
Example – without static
112/04/19 24
![Page 25: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/25.jpg)
Example – more
112/04/19 25
![Page 26: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/26.jpg)
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
![Page 27: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/27.jpg)
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
![Page 28: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/28.jpg)
Example of inner class
112/04/19 28
![Page 29: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/29.jpg)
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
![Page 30: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/30.jpg)
Example of generating error
112/04/19 30
![Page 31: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/31.jpg)
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
![Page 32: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/32.jpg)
Example
112/04/19 32
![Page 33: Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes](https://reader036.vdocuments.us/reader036/viewer/2022062321/56649da75503460f94a92a94/html5/thumbnails/33.jpg)
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