Chapter 2
Chapter 2
Chapter 2
មានពីរវិធីដើម្បីបង្កើត Objects
- Instantiate from class (new keyword)
- Use factory methods.
1.Class vs object
• What is a class? In object-oriented programming, a class is a blueprint for
creating objects (a particular data structure), providing initial values for state
(member variables or attributes), and implementations of behavior (member
functions or methods).A class is a blueprint that defined and store in a files.
• What is a Object ? In the object-oriented programming paradigm object can
be a combination of variables, functions, and data structures; in particular in
class-based flavour of the paradigm it refers to a particular instance of a
class.
Class vs Object
Disk
RAM
Object’s Members
An objects is encapsulated its member which can be data member or method
meber.
• A data member or properties is a variable of a class for storing value to
describe objects which instantiated from the class.
• A method is a sub program of a class for storing codes to for processing
data (do action) when it’s invoked(call) by object action.
2. class definition
[access] class className
{
[access] type member1;
[access] type member2;
[access] method definition
……
Constructors
}
Class’s components
• Access level : for class has public and default(not write any definition) for member have 4 access
level:
• private access in only its class
• default: access in only same package
• protected access in same package and outside package if it’s sub class
• public all
• Constructor is a special methods that run when instantiating. Use to assign initial value to data
member. In java if do not define constructors a default constructor is automatically created without
any parameter, however when we add user define the default is remove, you can keep it by create a
overload constructor.
• Methods: process: there are 2 type
• Typed method: method that it’s name hold value when finish process.(function)
access type methodName(par1,par,…)
{
code
reurn val
}
• Un-typed or void methods: method for doing something without return any value.
Typed use to find something
Un-typed use to do something.
• Parameter in java: មានពីទំរង់ call by val if primitive type & call by ref if object
type
(Cannot change)
overloading methods
• In java like other OOP methods can be overloaded. Overloading methods
are methods have same name but different parameters.
Different number of parameter or data type of parameter.
Overloading methods use one block location of memory to store codes of all
methods. Codes will be loaded over and over in memory when it’s called.
static members
• A static member is member declare by key word ”static” . Static members
store it’s value or code in a class (class encapsulation).
• Static member can access without object instantiate
• For data member(variable) it’s transparency value over objects.
• Static member is a feature inherit from structure programming of OOP.
NOTE: static member អាច call បានឬប្រើប្រាស់បានតែ static ដូចគ្នា ក្នុង Level ជាមួយគ្នា ។
3. Inheritance
ជាទំរង់ដែល classថ្មី (java ហៅថា sub class) មួយអាចប្រើប្រាស់រចនាសម្ព័ន្ធ
របស់ class ចាស់ដែលបាន compile រួច (ក្នុងjava ហៅថា super class )
ដោយបន្ថែម membersថ្មីៗ ហើយ Objects sub class បានទទួលលក្ខណៈ
Syntax
abstract methodName1(par,..);
abstract methodName2(par,..);
}
Interface
• Interface ជា special abstract class ដែលផ្ទុកទៅដោយ abstract methods.
• ចំនុចខុសគ្នា ចំបង រវាង abstract class vs interface
• ក្នុង Interface មានសុទ្ធតែ public abstract methods ចំនែក abstract class មានគ្ប់
ទាំងអស់។
• Methods របស់ Interface សុទ្ធតែ Abstract ដោយពុំប្ Keyword abstract ក្នុង abstract
class ត្វតែកំណត់ច្បាស់លាស់។
• ក្នុង Interface គ្ប់ abstract methods ជា public
• ដើម្បីបង្ខំអោយ sub class override methods interface គេប្ keyword
implements InterfaceName ខុសពី abstract class, class មួយអាច
implements interface ច្ន។
Interface
Syntax define Interface
access interface interfaceName
{
void methodName(par,…);
type methodName(par,…);
…….
}
Syntax implement interface
class ClassName extends SuperClass implements IntefaceName1,….{
overriding method implements
}
NOTE: Sub class ដែល Implement interface or abstract class ត្វOverride
all abstract methods otherwise
this class must be abstract too.
5. Final keyword.
final keyword ប្ក្នុង៣ទំរង់
៣ ទំ
រង់
• 1. ប្កាសថេរ
ថេរ !
• Syntax: final type VarName=Value;
• Example final String connection=“db\...”
• 2. កំណត់methodដើម្បីកំអោយគេ Override.
• Syntax: finale methodName(….){…..}
• 3. កំនត់ class កុំអោយគេ extends.
final class ClassName….{
Codes;
}
6. Package
• Package ជាឈ្មោះ មុខតំណររបស់ class និង Folder ជាទីតាំងសំរាប់ទុក
Classes.
• Package ប្បដូចជាទីតាំងកំណត់ដង្ហើមរបស់ classes.
• Syntax
package packageName;
class ClassName{…..}
• ដើម្បី ប្ប្ស់ class ក្នុង Package ប្keyword imports
import package.*; // use all classes in package
import package.ClassName; use only one class of package.