Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes.

Similar presentations


Presentation on theme: "Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes."— Presentation transcript:

1 Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes

2 Review Overloading Methods Using objects as parameters Argument passing Returning objects Recursion Access control Understanding static Nested and inner classes String class 2015/8/10 2

3 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. 2015/8/10 3 Class overload { void test() { //no argument } void test(int a) { //one argument } void test(int a, int b) { // two arguments }

4 Example of four methods 2015/8/10 4

5 Overloading Constructors Constructor is to initialize the values 2015/8/10 5

6 Example of three constructors 2015/8/10 6

7 Using Objects as parameters We could also pass objects to methods 2015/8/10 7

8 Example 2015/8/10 8

9 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. 2015/8/10 9

10 Call-by-value, there is no change in a and b in the lecture65{}, but change in test{} 2015/8/10 10

11 Call by object 2015/8/10 11

12 Returning the objects A method can return any type of data. The return type can be class types or values 2015/8/10 12

13 Example of returning an object, tmp 2015/8/10 13

14 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 2015/8/10 14

15 Example of recursion 2015/8/10 15

16 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 is fact(1) = 1 fact(n) = n*fact(n-1) 2015/8/10 16

17 Example of recursion, page 169 2015/8/10 17

18 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 2015/8/10 18

19 Example – difference between public and private 2015/8/10 19

20 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. 2015/8/10 20

21 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()). 2015/8/10 21

22 Example 2015/8/10 22

23 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. 2015/8/10 23

24 Example – without static 2015/8/10 24

25 Example – more 2015/8/10 25

26 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. 2015/8/10 26 A B

27 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. 2015/8/10 27

28 Example of inner class 2015/8/10 28

29 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. 2015/8/10 29

30 Example of generating error 2015/8/10 30

31 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); 2015/8/10 31

32 Example 2015/8/10 32

33 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 2015/8/10 33


Download ppt "Lecture From Chapter 6 & 7 2015/8/10 1 Method of Classes."

Similar presentations


Ads by Google