Presentation is loading. Please wait.

Presentation is loading. Please wait.

Method of Classes Chapter 7, page 155 Lecture 8 2019/4/6.

Similar presentations


Presentation on theme: "Method of Classes Chapter 7, page 155 Lecture 8 2019/4/6."— Presentation transcript:

1 Method of Classes Chapter 7, page 155 Lecture 8 2019/4/6

2 Review Overloading Methods Using objects as parameters
2019/4/6 Overloading Methods Using objects as parameters Argument passing Returning objects Recursion Access control Understanding static Nested and inner classes String class

3 Overloading Methods 2019/4/6 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. 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
2019/4/6

5 Overloading Constructors
2019/4/6 Constructor is to initialize the values

6 Example of three constructors
2019/4/6

7 Using Objects as parameters
2019/4/6 We could also pass objects to methods

8 Example 2019/4/6 ob1 will check with ob2 using equal(test o)

9 Argument passing 2019/4/6 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.

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

11 Call by object 2019/4/6

12 Returning the objects A method can return any type of data.
2019/4/6 A method can return any type of data. The return type can be class types or values

13 Example of returning an object, tmp
2019/4/6

14 Recursion 2019/4/6 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 = n(2) = n(1) + n(0) 3 = n(3) = n(2) + n(1) 5 = n(0) =1, n(1) =1

15 Example of recursion 2019/4/6

16 Recursion - factorial Fact(n) means n*(n -1)*(n – 2)*(n-3)…3*2*1
2019/4/6 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)

17 Example of recursion, page 169
2019/4/6 n = 1, 2, 3, 4, 5 fact = 1, 2, 6, 24, 120

18 Access control 2019/4/6 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

19 Example – difference between public and private
2019/4/6 cannot directly access through a method

20 As a is defined as default, we can access it.
Explanation 2019/4/6 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.

21 Understand static 2019/4/6 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()).

22 Example 2019/4/6 1 sequence of execution 2 4 3

23 Explanation 2019/4/6 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.

24 Example – without static
2019/4/6

25 Example – more 2019/4/6 output 1 4 2 3

26 A B Nested Classes We can define a class within another class.
2019/4/6 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. A B

27 More about nested class
2019/4/6 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.

28 Example of inner class 2019/4/6 2 3 1

29 Explanation The program will execute static void main()….
2019/4/6 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.

30 Example of generating error
2019/4/6 here, y is decared outside the inner class. Thus it is not known here

31 String class 2019/4/6 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 a DCO student.”); “I am a DCO student” is a String. It can be rewritten as: String name =“ I am a DCO student.” System.out.println(name);

32 Example 2019/4/6

33 Summary Overloading Methods – two or more methods with the same class
2019/4/6 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


Download ppt "Method of Classes Chapter 7, page 155 Lecture 8 2019/4/6."

Similar presentations


Ads by Google