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 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
YG - CS1709
10
YG - CS17011
YG - CS17012
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();
YG - CS17014
YG - CS17015
YG - CS17016
YG - CS17017
YG - CS17018
YG - CS17019
YG - CS17020
YG - CS17021
YG - CS17022
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();
YG - CS17024
YG - CS17025
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
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
YG - CS17028
YG - CS17029
YG - CS17030
YG - CS17031
YG - CS17032
YG - CS17033
YG - CS17034
YG - CS17035
YG - CS17036
YG - CS17037
YG - CS17038
YG - CS17039
YG - CS17040
YG - CS17041
YG - CS17042
YG - CS17043
YG - CS17044
YG - CS17045
YG - CS17046
YG - CS17047
YG - CS17048
YG - CS17049
YG - CS17050
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
YG - CS17052
YG - CS17053
YG - CS17054
YG - CS17055
YG - CS17056
YG - CS17057
YG - CS17058
YG - CS17059
YG - CS17060
YG - CS17061
YG - CS17062
YG - CS17063
YG - CS17064
YG - CS17065
YG - CS17066
YG - CS17067
YG - CS17068
YG - CS17069
YG - CS17070
YG - CS17071
YG - CS17072
YG - CS17073
YG - CS17074
YG - CS17075
YG - CS17076