Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.

Similar presentations


Presentation on theme: "CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2."— Presentation transcript:

1 CS170 ygao JAVA, C4Slide 1

2 CS170 ygao JAVA, C4Slide 2

3 CS170 ygao JAVA, C4Slide 3 Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally with the same scope of the accessibility in the memory Why encapsulation - Objects no longer need to be dependent upon any “outside” functions and data after the creation - Provide ADT (Abstract Data Types) - Easier in coding - Promote standards in OOP

4 CS170 ygao JAVA, C4Slide 4 Classes vs. Objects Class is a data structure that encapsulates data and functions. Data in a class are called member data, class variables, instance variables, or fields. Functions in a class are called member functions or methods. Constructors are special methods. Objects are the instance of a class. Creation of the objects and instantiation of the objects are all referring to memory allocation to hold the data and methods of the objects. In Java, all objects are created dynamically using new operator. For example: ClassName ObjName = new ClassName();

5 CS170 ygao JAVA, C4Slide 5 name /member data / methods

6 CS170 ygao JAVA, C4Slide 6 Note: Diagrams do not include methods, just for demo

7 CS170 ygao JAVA, C4Slide 7

8 CS170 ygao JAVA, C4Slide 8

9 CS170 ygao JAVA, C4Slide 9

10 CS170 ygao JAVA, C4Slide 10 Characteristics of Constructors A special method that has the same name of the class without return type, not even void. Constructor will be automatically called whenever an object is created. Like a method, constructor can be overloaded for the flexibility of the object creation. A super class constructor can be called by a sub class object as: super();

11 CS170 ygao JAVA, C4Slide 11

12 CS170 ygao JAVA, C4Slide 12

13 CS170 ygao JAVA, C4Slide 13 set methods In OOP, particularly in Java, set() means to set object’s member data set() usually has void return and takes argument(s) to set the member data set() also checks the validity of the data before it sets Use meaningful name for a set() method

14 CS170 ygao JAVA, C4Slide 14 get methods In OOP, particularly in Java, get() means to return one of object’s member data get() usually has a return type and takes no argument Use meaningful name for a get() method

15 CS170 ygao JAVA, C4Slide 15 Complete Example Analysis

16 CS170 ygao JAVA, C4Slide 16 Complete Example Analysis (continue)

17 CS170 ygao JAVA, C4Slide 17 Complete Example Analysis (continue)

18 CS170 ygao JAVA, C4Slide 18 Complete Example Analysis (continue)

19 CS170 ygao JAVA, C4Slide 19 Static Fields What is a static field? –a variable that is not associated with any instance of the class, therefore it’s a class-wide field/variable/data –a variable that can be used without creation of the object –a private static field can be only use within the current class –can be constant (final), so it’s value cannot be modified; must be initialized when declaring Why static fields? –keep tracking class-wide information –Convenient to use and save memory –Be careful data encapsulation in using static fields

20 CS170 ygao JAVA, C4Slide 20

21 CS170 ygao JAVA, C4Slide 21

22 CS170 ygao JAVA, C4Slide 22

23 CS170 ygao JAVA, C4Slide 23

24 CS170 ygao JAVA, C4Slide 24

25 CS170 ygao JAVA, C4Slide 25

26 CS170 ygao JAVA, C4Slide 26 Static Initialization Block what is a static initialization block? –a block of code in a class starting with keyword static –the block will be automatically executed when any method of the class is called first time why static initialization blocks? –used when a static object/field cannot be initialized by a single statement –ensure the object/filed is ready/available to the rest of the methods in the class starting at the point of any method in the class that is called

27 CS170 ygao JAVA, C4Slide 27

28 CS170 ygao JAVA, C4Slide 28 Simple Example of Static Block 1.public class TestStaticApp{ 2.private static int y;//declaring a static varaible 3.//private int x = 0; //illigal to delcare regular variable here in app 4.static { //static initialization block 5.y = 99;//initializing the static variable 6.} 7. public static void main(String[] args){ 8. System.out.println(“Output from main()"); 9. test(); // calling a method 10. } 11. public static void test() //must be static method in app 12. { 13. System.out.println("static field value in test():" + y); 14. } 15.}

29 CS170 ygao JAVA, C4Slide 29

30 CS170 ygao JAVA, C4Slide 30

31 CS170 ygao JAVA, C4Slide 31 Packages what is a package? –is used to organize the classes both API and programmer defined classes why package? –is able programs to import classes as API –provides convention for unique class names to avoid the name conflict

32 CS170 ygao JAVA, C4Slide 32

33 CS170 ygao JAVA, C4Slide 33

34 CS170 ygao JAVA, C4Slide 34

35 CS170 ygao JAVA, C4Slide 35

36 CS170 ygao JAVA, C4Slide 36

37 CS170 ygao JAVA, C4Slide 37 javadoc tool what is javadoc tool? –allows you to create documentation for your classes in the same way java API does –is a command in JDK that does the documentation why javadoc? –create professional-like documentation –Other programmers can use to learn about the fields, constructors, and methods in the classes

38 CS170 ygao JAVA, C4Slide 38

39 CS170 ygao JAVA, C4Slide 39

40 CS170 ygao JAVA, C4Slide 40

41 CS170 ygao JAVA, C4Slide 41


Download ppt "CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2."

Similar presentations


Ads by Google