Presentation is loading. Please wait.

Presentation is loading. Please wait.

YG - CS1701. 2 3 4 5 6 7 8 Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.

Similar presentations


Presentation on theme: "YG - CS1701. 2 3 4 5 6 7 8 Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally."— Presentation transcript:

1 YG - CS1701

2 2

3 3

4 4

5 5

6 6

7 7

8 8 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

9 YG - CS1709

10 10

11 YG - CS17011

12 YG - CS17012

13 YG - CS17013 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();

14 YG - CS17014

15 YG - CS17015

16 YG - CS17016

17 YG - CS17017

18 YG - CS17018

19 YG - CS17019

20 YG - CS17020

21 YG - CS17021

22 YG - CS17022

23 YG - CS17023 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();

24 YG - CS17024

25 YG - CS17025

26 YG - CS17026 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 get methods

27 YG - CS17027 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 set methods

28 YG - CS17028

29 YG - CS17029

30 YG - CS17030

31 YG - CS17031

32 YG - CS17032

33 YG - CS17033

34 YG - CS17034

35 YG - CS17035

36 YG - CS17036

37 YG - CS17037

38 YG - CS17038

39 YG - CS17039

40 YG - CS17040

41 YG - CS17041

42 YG - CS17042

43 YG - CS17043

44 YG - CS17044

45 YG - CS17045

46 YG - CS17046

47 YG - CS17047

48 YG - CS17048

49 YG - CS17049

50 YG - CS17050

51 YG - CS17051 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

52 YG - CS17052

53 YG - CS17053

54 YG - CS17054

55 YG - CS17055

56 YG - CS17056

57 YG - CS17057

58 YG - CS17058

59 YG - CS17059

60 YG - CS17060

61 YG - CS17061

62 YG - CS17062

63 YG - CS17063

64 YG - CS17064

65 YG - CS17065

66 YG - CS17066

67 YG - CS17067

68 YG - CS17068

69 YG - CS17069

70 YG - CS17070

71 YG - CS17071

72 YG - CS17072

73 YG - CS17073

74 YG - CS17074

75 YG - CS17075

76 YG - CS17076


Download ppt "YG - CS1701. 2 3 4 5 6 7 8 Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally."

Similar presentations


Ads by Google