Java Presentation
មជ្ឈមណ្ឌ លកូរ ៉េ សហ្វ វែ រេច េ ឌី
Korea Software HRD Center
www.kshrd.com.kh
ក្ក ុមទី៤៖ រោក ជ្ឹម មិនា
រោក ជាគីមហាែ
រោក​ហ្៊ុំ ជ្ីែ័ន
រោក សង គង្គា
រោក គីម បនហ្ង
ថ្នា ក់ ភ្ាំរេញ ក្ក ុមទី ៤
ក្បធានបទ៖ មូលដ្ឋា នគ្រឹះរបស់ភាសាJava
សមាជ្ិក
លោក ជាគីមហាវ
លោក​ហ៊ុំ ជីវ័ន
លោក សង គង្គា
លោក គីម បនហង
មាតិកា
1. ល ើអ្វីជា OOP?
2. ល ើអ្វីជា​Class និង Object ?
3. ល ើអ្វីជា Encapsulation ?
4. ល ើអ្វីជា Inheritance?
5. ល ើអ្វីជា Abstraction (Abstract class Interface)
6. ល ើអ្វីជា Polymorphism ?
3
4
OOP
 ពាកយថា OOP មានន័យថា Object Oriented Programming ល ើមបីជួយល ោះស្រសយលៅបញ្ហា ទាំងឡាយ។
 មូល ា នស្រគរោះរបស់ OOP គឺជា​class ល ើមបីសា ល់ថា Program មួយមានលកខណៈជា OOP
លោះស្រាត Program ល ោះបាំលេញនូវលកខខណឌ ៣យ៉ា ងគឺ៖
ល ើអ្វីលៅជា OOP ?
1. Encapsulation
2. Inheritance
3. Polymorphism
5
OOP
 Class គឺជាេមពគាំរូសស្រមាប់បលងកើ Object ឬ​កាំណ ់ទស្រមង់របស់ Object ។
 Java លស្របើលកខណៈេិលសសរបស់ Class ល ើមបីបលងកើ Object ។
ល ើអ្វីលៅជា Class ?
 Instance Variable ( Data Member ) និង Method ត ល
Instance Variable និង Method គឺជា Member របស់ Class។
6
OOP
class class-name{
type instance-variable1;
type instance-variable2;
//….
type instance-variableN;
type methodname 1(parameter-list){
//body of method
}
type methodname 2(parameter-list){
//body of method
}
type methodname N(parameter-list){
//body of method
}
ទាំរង់ទូលៅរបស់ Class
7
OOP
Class Vehicle{
int passengers; //number of passengers
int fuelcap; //fuel capacity in liters
int kpl; //fuel consumption in kilometers per liter
}
ការបលងកើ Class
o Class អាចបលងកើ នូវស្របលេទទិននន័យ (Data type) ថ្មីមួយ ។ កន ុងករណីលនោះ ស្របលេទ
ទិននន័យថ្មីល ោះល្ម ោះថា​ Vehicle លហើយលយើងនរងលស្របើល្ម ោះលនោះល ើមបីស្របកាស
Objects ។
Vehicle minivan = new Vehicle();
OOP
 Object គឺជា Instance របស់ Class។
 Object ជាអ្វីៗត លលៅជាំវ ិញខល ួនលយើង ូចជា កង់ ម៉ាូ ូ​ឡាន លសៀវលៅ ប ៊ិច កាំេយូ ទ័រ…។
ល ើអ្វីលៅជា Object ?
 Object មាន State និង Behavior។
 State ជាលកខណៈ ឬ ធា ផ្សាំសស្រមាប់បលងកើ Object ។
Ex1: មនសសមាន សមបុ រ កាំេស់ អាយ លេទ ។
Ex2:ម៉ាូ ូមាន កង់ តកប ស្រចវ៉ា ក់ លេលើងសីញ៉ាូ…។
 Behavior ជាអ្វីត ល Object អាចល្វើបាន។
Ex1: មនសសមាន ល ើរ ល ក អ្ងគ ុយ ឈរ យាំ លសើច….។
Ex2:ទូរ ATM មាន កស្របាក់ ក់ស្របាក់ …. ។
9
OOP
រលបៀបបលងកើ Objects
Vehicle minivan = new Vehicle();
• លយើងស្របកាសអ្ញ្ញ ិតមួយមានល្ម ោះថា minivan ត លមាន Vehicle ជាស្របលេទ
ទិននន័យ (Reference of Object)។
• new operator បលងកើ ទីាាំងកន ុង​Memory របស់ Object មួយលហើយផ្តល់
Reference លៅឲ្យ Object ល ោះ ។
• Reference ជាអាស័យ ា ននន Object លៅកន ុង Memory ត លបលងកើ ទីាាំងល យ
new ។
10
OOP
រលបៀបបលងកើ Methods
ret-type methName(param-list){
//body of method
}
 ret-type បញ្ហា ក់េីស្របលេទទិននន័យត លស្រ ូវបានផ្តល់ឲ្យ Method ។ វអាចជា
ស្របលេទទិននន័យ្មមា ឬ អាចស្របលេទជា​Class ត លលយើងបានបលងកើ ល ើង ។
 Method មិនស្រ ូវការផ្តល់ នមលលទ ល ោះស្របលេទទិននន័យរបស់វស្រ ូវត ជា void ។
 methName ជាល្ម ោះរបស់ Method ។
 param-list ជាស្របលេទទិននន័យ និង ល្ម ោះអ្ញ្ញ តិលស្របើបនតគ្នន ត លនរងតញកផ្តត ច់េីគ្នន ល យ
សញ្ហញ លកបៀស (,)។
11
Encapsulation
 Encapsulation វជាតផ្នកមួយនន OOP concept។
 ល្វើលអាយ Field លៅកន ុង class មួយមិនអ្នញ្ហញ លអាយមានការ access
ចូលេី class នទ។លពាលគឺ variable លៅកន ុង class មួយស្រ ូវបាន hide
េី class នទ លហើយវអាច access បានាមរយៈ method getter()
និង​setter()។
ល ើអ្វីលៅជា Encapsulation ?
12
Encapsulation
 Field របស់ class អាចកាំណ ់ជា Read-only ឬ Write-only.
 Class អាចស្រគប់ស្រគងទាំងស្រស ុងលៅលលើ data ល យលស្របើស្របាស់ code។
គណសមប តិរបស់ Encapsulation ?
 អ្នកលស្របើស្របាស់ class មិន រងេីរលបៀបត ល class ផ្ទ ុក(store) ទិនទន័យ។
 Class អាចបត ូរនូវ data type នន field មួយ។
 អ្នកលស្របើស្របាស់់ class មិនស្រ ូវការបត ូរ code របស់លគ។
13
Encapsulation
public class TestJava {
private int id;
private String name;
private int age;
public void setId(int id){this.id=id;}
public int getID(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
public void setAge(int age){
if(age<0){
this.age=1;
}else{
this.age=age;
}
}
public int getAge(){return age;}
}
Public class DemoEncap{
public static void main(String args[]){
TestJava obj= new TestJava();
obj.setId(100);
obj.setName("Hong");
obj.setAge(-1);
System.out.println("ID:"+
obj.getID() + "nName:"+ obj.getName()+
"nAge:"+ obj.getAge());
}
}
14
Inheritance
ល ើអ្វីលៅជា Inheritance?
 Inheritance គឺជាលផ្ទរមរ កេី Class មួយលៅ Class មួយលផ្សងលទៀ ។
 Class ត លលផ្ទរ នមលលអាយលគលៅថា Parent Class លហើយ Class ត លទទួល នមលលៅថា Child Class។
 Inheritance អ្នញ្ហញ ិឲ្យ Class មួយលស្របើស្របាស់លៅ properties និង methods នន class លផ្សងលទៀ បាន ។
 Inheritance គឺជា compile-time mechanism ។ super-class អាចមាន subclasses ននមួយចាំនួន។
ប៉ាតនត subclass មានស្រ រមត super-class មួយប៉ាល្ណ ោះ ។
15
Inheritance
ល ើ Inheritance ល្វើការយ៉ា ង ូចលមតច?
 Extends: លៅកន ុងJava ល ើមបីល្វើការ Inheritance េី Parent Class លៅChild Class លយើងស្រ ូវលស្របើស្របាស់
Keyword Extends។
 Super: លគលស្របើវសស្រមាប់កាំណ ់លកខណៈMemberរបស់ Classលម ករណី ត ល Method និង Variable
របស់ Classលមនិង Classកូន ូចគ្នន ។
 Implement: ជាKeyword មួយត លអ្នញ្ហញ លអាយ Class ល្វើការទទួលមរ កេី Interface។
16
Inheritance
class ClassA{
int a=;
public void add(int x, int y){
a = x+y;
System.out.println("The sum of the given numbers:"+a);
}}
public class ClassB extends ClassA{
public void multiplication(int x, int y){
a= x*y;
System.out.println("The product of the given
numbers:"+a);
}
public static void main(String args[]){
int a = 20, b = 10;
ClassB sub = new ClassB ();
sub.add(a, b);
sub.multiplication(a, b);
}
}
 ឧទហរណៈ លយើងមាន Classេីរ គឺ
ClassA និងClassB ត លClassBល្វើការ
Inheritannce នមលេី ClassA។
17
Inheritance
 ឧទហរណៈ
public class Sub_class extends Super_class {
int num = 10; //display method of sub class
public void display(){
System.out.println("This is the display method of subclass");}
public void my_method(){
Sub_class sub = new Sub_class();//Instantiating subclass
sub.display();//Invoking the display() method of sub class
super.display();//Invoking the display() method of superclass
System.out.println("value of the variable named num in sub class:"+ sub.num);
//printing the value of variable num of superclass
System.out.println("value of the variable named num in super class:"+ super.num);
}
public static void main(String args[]){
Sub_class obj = new Sub_class();
obj.my_method();
}
class Super_class{
int num = 20; //display method of superclass
public void display(){
System.out.println("This is the display method
of superclass");}}
18
Inheritance
 ឧទហរណៈ Public Interface Motor{
void speedUp(int increment);
}
class MyMotor implement Motor{
int speed = 0;
void speedUp(int increment) {
speed = speed + increment;
}
void printStates() {
System.out.println("My motor speed" +speed);
}
}
19
Inheritance
ស្របលេទនន Inheritance
 Single inheritance in java
 multilevel inheritance
 multiple inheritance
 hybrid inheritance
 hierarchical inheritance
20
Abstraction
 រាល់ programming languages ត ងត ផ្តល់ abstractions ។
 វជួយផ្តល់លៅ ាំល្ោះស្រសយត លមានភាេសម ុគសម ញននបញ្ហា (ល ោះស្រសយ ល់ផ្តទ ល់លៅាមស្របលេទ
និង គណសមប តិនន abstraction ។
ល ើអ្វីលៅជា Abstraction?
 Abstraction វជាធា សាំខាន់មួយនន object-oriented programming
 Abstraction ជា ាំលណើរការននការោក់បាាំងលសចកតីលាំអ្ិ ននការអ្នវ តន៍( implementation)លហើយបង្គា ញត
ួ ទី ត មួយគ ់លៅ ល់អ្នកលស្របើស្របាស់។
21
Abstraction
 Abstract class ជា​class មួយត លស្របកាសជា abstract ។
 Abstract method ្ឺជា methodមួយត លស្របកាសជា abstract ។
ល ើអ្វីលៅជា Abstract class?
 Abstract Class ករកាទកលៅ abstract method មួយឬលស្រចើន។
Syntax: abstract class (class name){
//abstract methods
}
22
Abstraction
ចាំ្ាំ៖
o គបសិនជាមាន abstract method ចគរើនចៅក្នុង class តែមួយ
ច ឹះ class គែូវតែជា abstract ។
o គបសិនជាអ្នក្ក្ំពុងតែ extend abstract classចគរើនតែលមាន abstract method
អ្នក្មិនគែូវផ្តល់ការ implementationចៅចោយ method ឬ ច្វើចោយ class ច ឹះ
កាា យជា abstract class។
23
Abstraction
ល ើអ្វីលៅជា Interface ?
 Interface លមើលលៅ ូចជា class ប៉ាតនតវមិនតមនជា class លទ។
 Interface គឺលស្របើលៅ default abstract (មានស្រ រមត method signature គ្នម ន body លទ)។
 Interface ល ើរ ួ ទីជា blueprint (បលង់)សាំរាប់ class ។
 Interface មិនអាចបលងកើ object បានលទ។
24
Abstraction
លហ អ្វីលយើងស្រ ូវលស្របើ Interface ?
 ល្វើឲ្យ abstract មួយ​​​មានលកខណៈលេញលលញ ។
 ាមរយៈ interface លយើងអាចស្រប ិប តិការមខង្គររបស់ multi-inheritance។
 វអាចស្រ ូវលស្របើស្របាស់ល ើមបីបាំលេញ loose coupling ។
25
Abstraction
1. Abstract class VS Interface:
ភាេដូចគ្នា វាង​Abstract class & Interface:
 អាចឲ្យ abstraction និងអាចស្របកាស abstract method ។
 Abstract class & Interface មិនអាចជា instantiated។
26
Abstraction
Abstract class Interface
1) Abstract class អាចមានឬគ្នម ន(0%-100%)
abstract method។
Interface ស្រ ូវត មាន (100%) abstract method។
2) Abstract class មិន​​support multiple
inheritance។
Interface supports multiple inheritance.
3) Abstract class អាចមាន final, non-final,
static and non-static variables។
Interface អាចមាន static and final variables ប៉ាល្ណ ោះ។
4) Abstract class អាចមាន static methods,
main method and constructor។
Interface មិនមាន static methods, main method and
Constructor។
5) Abstract class អាចផ្តល់​implementation
របស់interface។
Interface មិនអាចផ្តល់​implementation របស់ abstract class
បានលទ។
6) The abstract keyword គឺលស្របើល ើមបីស្របកាស់
abstract class។
The Interface keyword គឺលស្របើល ើមបីស្របកាស់ Interface។
7) Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
ភាេខសគ្នា វាង​Abstract class & Interface:
27
Polymorphism
 Polymorphism មកេីភាសលស្រកក​េីរពាកយគឺ poly និង morphs ។
 Poly មានន័យថា​លស្រចើន និង morphs មានន័យថា ទាំរង់ ។
 polymorphism មានេីរស្របលេទលៅកន ុង java គឺ
1) compile time polymorphism(static polymorphism)
2) runtime polymorphism (Dynamic polymorphism )
ល ើអ្វីលៅជា Polymorphism?
 លយើងអាចអ្នវ ត polymorphism លៅកន ុង java ល យ
method overloading និង method overriding ។
28
Polymorphism
ល ើអ្វីលៅជា Runtime Polymorphism ?
 Method overriding គឺជាឧទហរណ៍ លអ ចាំលពាោះ runtime polymorphism ។
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
 លាោះលយើងលៅលមើលឧទហរណ៍ល ើមបីយល់កាន់ចាស់
public class X {
public void methodA() //Base class method
{ System.out.println
("hello, I'm methodA of class X");
}
}
public class Y extends X {
public void methodA() //Derived Class method
{ System.out.println ("hello, I'm methodA of class Y");
}
}
public class Z {
public static void main (String args []) {
X obj1 = new X(); // Reference and object X
X obj2 = new Y(); // X reference but Y object
obj1.methodA();
obj2.methodA();
}
}
Output:
hello, I'm
methodA of
class X
hello, I'm
methodA of
class Y
29
Polymorphism
ល ើអ្វីលៅជា Compile time Polymorphism ?
 Method overloading គឺជាឧទហរណ៍ លអ ចាំលពាោះ compile time polymorphism ។
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
 លាោះលយើងលៅលមើលឧទហរណ៍ល ើមបីយល់កាន់ចាស់
class X {
void methodA(int num) {
System.out.println ("methodA:" + num);
}
void methodA(int num1, int num2)
{
System.out.println ("methodA:" + num1 + "," + num2);
} double methodA(double num) {
System.out.println("methodA:" + num); return num;
}
} class Y {
public static void main (String args []) {
X Obj = new X(); double result; Obj.methodA(20);
Obj.methodA(20, 30); result = Obj.methodA(5.5);
System.out.println("Answer is:" + result);
}
}
Output:
methodA:20
methodA:20,30
methodA:5.5
Answer is:5.5
30
Polymorphism
 អ្នញ្ហញ ិឲ្យ interface មួយអាចស្រ ូវបានលស្របើស្របាស់ជាទូលៅលៅកន ុង class។
 ស្រប ិប តិការមួយអាចមាន behavior ខសគ្នន លៅកន ុងករណីលផ្សងគ្នន ។
ល ើ Polymorphism ល្វើការយ៉ា ង ូចលមតច?
 Behavior េរងតផ្អកលៅលលើស្របលេទនន data ត លបានលស្របើលៅកន ុងលេលអ្នវ ត។
 វល ើរ ួ ទីយ៉ា ងសាំខាន់លៅកន ុងការអ្នញ្ហញ ិឲ្យ object ត លមាន internal structures
ល ើមបីតចករំតលក external interface ។
 Polymorphism ស្រ ូវបានលគលស្របើយ៉ា ងទូលាំទូោយកន ុងការ implement inheritance ។
31
Polymorphism
ល ើអ្វីជា​Method Overloading ?
 Method Overloading មានល្ម ោះ ូចគ្នន ​ត ខសគ្នន លៅចាំនួន និង data type នន parameter ។
class Overload
{
void demo (int a) {
System.out.println ("a: " + a);
}
void demo (int a,int b){
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a); return a*a;
}
}
class MethodOverloading {
public static void main (String args []) {
Overload Obj = new Overload(); double result; Obj .demo(10); Obj .demo(10, 20);
result = Obj .demo(5.5); System.out.println("O/P : " + result);
}
}
Output:
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25
32
Polymorphism
ល ើ Method Overloading ល្វើការយ៉ា ង ូចលមតច ?
1. Method Overloading អាចល្វើការលៅកន ុង class ូចគ្នន ឬ លៅកន ុង sub-class របស់វ ។
2. Constructor លៅកន ុង Java អាចជា Overloaded
3. Overloaded methods គួរត មាន argument list ខសគ្នន
4. Parameter អាចខសគ្នន លៅលលើ type ឬ number ឬក ទាំងេីរត មតង ។
5. េួកវអាចមាន return types ខសគ្នន ឬ ូចគ្នន ។
6. វកស្រ ូវបានលគលៅថាជា compile time polymorphism ។
33
Polymorphism
ល ើអ្វីជា​Method Overriding ?
 Method Overriding មានល្ម ោះ ូចគ្នន ​ចាំនួន និង data type នន parameter ក ូចគ្នន ។
public class BaseClass { public void methodToOverride() //Base class method
{
System.out.println ("I'm the method of BaseClass");
}
}
public class DerivedClass extends BaseClass {
public void methodToOverride() //Derived Class method
{
System.out.println ("I'm the method of DerivedClass");
}
}
public class TestMethod {
public static void main (String args []) { // BaseClass reference and object
BaseClass obj1 = new BaseClass(); // BaseClass reference but DerivedClass object
BaseClass obj2 = new DerivedClass(); // Calls the method from BaseClass class
obj1.methodToOverride(); //Calls the method from DerivedClass class obj2.methodToOverride();
}
}
Output:
I'm the method of BaseClass I'm the method
of DerivedClass
34
Polymorphism
ល ើ Method Overriding ល្វើការយ៉ា ង ូចលមតច ?
1. អ្នញ្ហញ ិ ត ចាំលពាោះ inherited methods
2. Object type (មិនតមនជា reference variable type)ល យកាំណ ់
overridden method និងស្រ ូវបានលស្របើលៅលេល runtime
3. Overriding method អាចមាន return type ខសគ្នន
4. Overriding method មិនស្រ ូវមាន restrictive access modifier ។
5. Abstract methods ស្រ ូវបានអាច overridden
6. Static និង final method មិនអាច overridden
7. វស្រ ូវបានលគសា ល់ថាជា Runtime polymorphism ។
10. ក្បភ្េឯកស
35
សូមអ្រ្ុណ!!!
36

OOP Concept

  • 1.
    Java Presentation មជ្ឈមណ្ឌ លកូរ៉េ សហ្វ វែ រេច េ ឌី Korea Software HRD Center www.kshrd.com.kh ក្ក ុមទី៤៖ រោក ជ្ឹម មិនា រោក ជាគីមហាែ រោក​ហ្៊ុំ ជ្ីែ័ន រោក សង គង្គា រោក គីម បនហ្ង
  • 2.
    ថ្នា ក់ ភ្ាំរេញក្ក ុមទី ៤ ក្បធានបទ៖ មូលដ្ឋា នគ្រឹះរបស់ភាសាJava សមាជ្ិក លោក ជាគីមហាវ លោក​ហ៊ុំ ជីវ័ន លោក សង គង្គា លោក គីម បនហង
  • 3.
    មាតិកា 1. ល ើអ្វីជាOOP? 2. ល ើអ្វីជា​Class និង Object ? 3. ល ើអ្វីជា Encapsulation ? 4. ល ើអ្វីជា Inheritance? 5. ល ើអ្វីជា Abstraction (Abstract class Interface) 6. ល ើអ្វីជា Polymorphism ? 3
  • 4.
    4 OOP  ពាកយថា OOPមានន័យថា Object Oriented Programming ល ើមបីជួយល ោះស្រសយលៅបញ្ហា ទាំងឡាយ។  មូល ា នស្រគរោះរបស់ OOP គឺជា​class ល ើមបីសា ល់ថា Program មួយមានលកខណៈជា OOP លោះស្រាត Program ល ោះបាំលេញនូវលកខខណឌ ៣យ៉ា ងគឺ៖ ល ើអ្វីលៅជា OOP ? 1. Encapsulation 2. Inheritance 3. Polymorphism
  • 5.
    5 OOP  Class គឺជាេមពគាំរូសស្រមាប់បលងកើObject ឬ​កាំណ ់ទស្រមង់របស់ Object ។  Java លស្របើលកខណៈេិលសសរបស់ Class ល ើមបីបលងកើ Object ។ ល ើអ្វីលៅជា Class ?  Instance Variable ( Data Member ) និង Method ត ល Instance Variable និង Method គឺជា Member របស់ Class។
  • 6.
    6 OOP class class-name{ type instance-variable1; typeinstance-variable2; //…. type instance-variableN; type methodname 1(parameter-list){ //body of method } type methodname 2(parameter-list){ //body of method } type methodname N(parameter-list){ //body of method } ទាំរង់ទូលៅរបស់ Class
  • 7.
    7 OOP Class Vehicle{ int passengers;//number of passengers int fuelcap; //fuel capacity in liters int kpl; //fuel consumption in kilometers per liter } ការបលងកើ Class o Class អាចបលងកើ នូវស្របលេទទិននន័យ (Data type) ថ្មីមួយ ។ កន ុងករណីលនោះ ស្របលេទ ទិននន័យថ្មីល ោះល្ម ោះថា​ Vehicle លហើយលយើងនរងលស្របើល្ម ោះលនោះល ើមបីស្របកាស Objects ។ Vehicle minivan = new Vehicle();
  • 8.
    OOP  Object គឺជាInstance របស់ Class។  Object ជាអ្វីៗត លលៅជាំវ ិញខល ួនលយើង ូចជា កង់ ម៉ាូ ូ​ឡាន លសៀវលៅ ប ៊ិច កាំេយូ ទ័រ…។ ល ើអ្វីលៅជា Object ?  Object មាន State និង Behavior។  State ជាលកខណៈ ឬ ធា ផ្សាំសស្រមាប់បលងកើ Object ។ Ex1: មនសសមាន សមបុ រ កាំេស់ អាយ លេទ ។ Ex2:ម៉ាូ ូមាន កង់ តកប ស្រចវ៉ា ក់ លេលើងសីញ៉ាូ…។  Behavior ជាអ្វីត ល Object អាចល្វើបាន។ Ex1: មនសសមាន ល ើរ ល ក អ្ងគ ុយ ឈរ យាំ លសើច….។ Ex2:ទូរ ATM មាន កស្របាក់ ក់ស្របាក់ …. ។
  • 9.
    9 OOP រលបៀបបលងកើ Objects Vehicle minivan= new Vehicle(); • លយើងស្របកាសអ្ញ្ញ ិតមួយមានល្ម ោះថា minivan ត លមាន Vehicle ជាស្របលេទ ទិននន័យ (Reference of Object)។ • new operator បលងកើ ទីាាំងកន ុង​Memory របស់ Object មួយលហើយផ្តល់ Reference លៅឲ្យ Object ល ោះ ។ • Reference ជាអាស័យ ា ននន Object លៅកន ុង Memory ត លបលងកើ ទីាាំងល យ new ។
  • 10.
    10 OOP រលបៀបបលងកើ Methods ret-type methName(param-list){ //bodyof method }  ret-type បញ្ហា ក់េីស្របលេទទិននន័យត លស្រ ូវបានផ្តល់ឲ្យ Method ។ វអាចជា ស្របលេទទិននន័យ្មមា ឬ អាចស្របលេទជា​Class ត លលយើងបានបលងកើ ល ើង ។  Method មិនស្រ ូវការផ្តល់ នមលលទ ល ោះស្របលេទទិននន័យរបស់វស្រ ូវត ជា void ។  methName ជាល្ម ោះរបស់ Method ។  param-list ជាស្របលេទទិននន័យ និង ល្ម ោះអ្ញ្ញ តិលស្របើបនតគ្នន ត លនរងតញកផ្តត ច់េីគ្នន ល យ សញ្ហញ លកបៀស (,)។
  • 11.
    11 Encapsulation  Encapsulation វជាតផ្នកមួយននOOP concept។  ល្វើលអាយ Field លៅកន ុង class មួយមិនអ្នញ្ហញ លអាយមានការ access ចូលេី class នទ។លពាលគឺ variable លៅកន ុង class មួយស្រ ូវបាន hide េី class នទ លហើយវអាច access បានាមរយៈ method getter() និង​setter()។ ល ើអ្វីលៅជា Encapsulation ?
  • 12.
    12 Encapsulation  Field របស់class អាចកាំណ ់ជា Read-only ឬ Write-only.  Class អាចស្រគប់ស្រគងទាំងស្រស ុងលៅលលើ data ល យលស្របើស្របាស់ code។ គណសមប តិរបស់ Encapsulation ?  អ្នកលស្របើស្របាស់ class មិន រងេីរលបៀបត ល class ផ្ទ ុក(store) ទិនទន័យ។  Class អាចបត ូរនូវ data type នន field មួយ។  អ្នកលស្របើស្របាស់់ class មិនស្រ ូវការបត ូរ code របស់លគ។
  • 13.
    13 Encapsulation public class TestJava{ private int id; private String name; private int age; public void setId(int id){this.id=id;} public int getID(){return id;} public void setName(String name){this.name=name;} public String getName(){return name;} public void setAge(int age){ if(age<0){ this.age=1; }else{ this.age=age; } } public int getAge(){return age;} } Public class DemoEncap{ public static void main(String args[]){ TestJava obj= new TestJava(); obj.setId(100); obj.setName("Hong"); obj.setAge(-1); System.out.println("ID:"+ obj.getID() + "nName:"+ obj.getName()+ "nAge:"+ obj.getAge()); } }
  • 14.
    14 Inheritance ល ើអ្វីលៅជា Inheritance? Inheritance គឺជាលផ្ទរមរ កេី Class មួយលៅ Class មួយលផ្សងលទៀ ។  Class ត លលផ្ទរ នមលលអាយលគលៅថា Parent Class លហើយ Class ត លទទួល នមលលៅថា Child Class។  Inheritance អ្នញ្ហញ ិឲ្យ Class មួយលស្របើស្របាស់លៅ properties និង methods នន class លផ្សងលទៀ បាន ។  Inheritance គឺជា compile-time mechanism ។ super-class អាចមាន subclasses ននមួយចាំនួន។ ប៉ាតនត subclass មានស្រ រមត super-class មួយប៉ាល្ណ ោះ ។
  • 15.
    15 Inheritance ល ើ Inheritanceល្វើការយ៉ា ង ូចលមតច?  Extends: លៅកន ុងJava ល ើមបីល្វើការ Inheritance េី Parent Class លៅChild Class លយើងស្រ ូវលស្របើស្របាស់ Keyword Extends។  Super: លគលស្របើវសស្រមាប់កាំណ ់លកខណៈMemberរបស់ Classលម ករណី ត ល Method និង Variable របស់ Classលមនិង Classកូន ូចគ្នន ។  Implement: ជាKeyword មួយត លអ្នញ្ហញ លអាយ Class ល្វើការទទួលមរ កេី Interface។
  • 16.
    16 Inheritance class ClassA{ int a=; publicvoid add(int x, int y){ a = x+y; System.out.println("The sum of the given numbers:"+a); }} public class ClassB extends ClassA{ public void multiplication(int x, int y){ a= x*y; System.out.println("The product of the given numbers:"+a); } public static void main(String args[]){ int a = 20, b = 10; ClassB sub = new ClassB (); sub.add(a, b); sub.multiplication(a, b); } }  ឧទហរណៈ លយើងមាន Classេីរ គឺ ClassA និងClassB ត លClassBល្វើការ Inheritannce នមលេី ClassA។
  • 17.
    17 Inheritance  ឧទហរណៈ public classSub_class extends Super_class { int num = 10; //display method of sub class public void display(){ System.out.println("This is the display method of subclass");} public void my_method(){ Sub_class sub = new Sub_class();//Instantiating subclass sub.display();//Invoking the display() method of sub class super.display();//Invoking the display() method of superclass System.out.println("value of the variable named num in sub class:"+ sub.num); //printing the value of variable num of superclass System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]){ Sub_class obj = new Sub_class(); obj.my_method(); } class Super_class{ int num = 20; //display method of superclass public void display(){ System.out.println("This is the display method of superclass");}}
  • 18.
    18 Inheritance  ឧទហរណៈ PublicInterface Motor{ void speedUp(int increment); } class MyMotor implement Motor{ int speed = 0; void speedUp(int increment) { speed = speed + increment; } void printStates() { System.out.println("My motor speed" +speed); } }
  • 19.
    19 Inheritance ស្របលេទនន Inheritance  Singleinheritance in java  multilevel inheritance  multiple inheritance  hybrid inheritance  hierarchical inheritance
  • 20.
    20 Abstraction  រាល់ programminglanguages ត ងត ផ្តល់ abstractions ។  វជួយផ្តល់លៅ ាំល្ោះស្រសយត លមានភាេសម ុគសម ញននបញ្ហា (ល ោះស្រសយ ល់ផ្តទ ល់លៅាមស្របលេទ និង គណសមប តិនន abstraction ។ ល ើអ្វីលៅជា Abstraction?  Abstraction វជាធា សាំខាន់មួយនន object-oriented programming  Abstraction ជា ាំលណើរការននការោក់បាាំងលសចកតីលាំអ្ិ ននការអ្នវ តន៍( implementation)លហើយបង្គា ញត ួ ទី ត មួយគ ់លៅ ល់អ្នកលស្របើស្របាស់។
  • 21.
    21 Abstraction  Abstract classជា​class មួយត លស្របកាសជា abstract ។  Abstract method ្ឺជា methodមួយត លស្របកាសជា abstract ។ ល ើអ្វីលៅជា Abstract class?  Abstract Class ករកាទកលៅ abstract method មួយឬលស្រចើន។ Syntax: abstract class (class name){ //abstract methods }
  • 22.
    22 Abstraction ចាំ្ាំ៖ o គបសិនជាមាន abstractmethod ចគរើនចៅក្នុង class តែមួយ ច ឹះ class គែូវតែជា abstract ។ o គបសិនជាអ្នក្ក្ំពុងតែ extend abstract classចគរើនតែលមាន abstract method អ្នក្មិនគែូវផ្តល់ការ implementationចៅចោយ method ឬ ច្វើចោយ class ច ឹះ កាា យជា abstract class។
  • 23.
    23 Abstraction ល ើអ្វីលៅជា Interface?  Interface លមើលលៅ ូចជា class ប៉ាតនតវមិនតមនជា class លទ។  Interface គឺលស្របើលៅ default abstract (មានស្រ រមត method signature គ្នម ន body លទ)។  Interface ល ើរ ួ ទីជា blueprint (បលង់)សាំរាប់ class ។  Interface មិនអាចបលងកើ object បានលទ។
  • 24.
    24 Abstraction លហ អ្វីលយើងស្រ ូវលស្របើInterface ?  ល្វើឲ្យ abstract មួយ​​​មានលកខណៈលេញលលញ ។  ាមរយៈ interface លយើងអាចស្រប ិប តិការមខង្គររបស់ multi-inheritance។  វអាចស្រ ូវលស្របើស្របាស់ល ើមបីបាំលេញ loose coupling ។
  • 25.
    25 Abstraction 1. Abstract classVS Interface: ភាេដូចគ្នា វាង​Abstract class & Interface:  អាចឲ្យ abstraction និងអាចស្របកាស abstract method ។  Abstract class & Interface មិនអាចជា instantiated។
  • 26.
    26 Abstraction Abstract class Interface 1)Abstract class អាចមានឬគ្នម ន(0%-100%) abstract method។ Interface ស្រ ូវត មាន (100%) abstract method។ 2) Abstract class មិន​​support multiple inheritance។ Interface supports multiple inheritance. 3) Abstract class អាចមាន final, non-final, static and non-static variables។ Interface អាចមាន static and final variables ប៉ាល្ណ ោះ។ 4) Abstract class អាចមាន static methods, main method and constructor។ Interface មិនមាន static methods, main method and Constructor។ 5) Abstract class អាចផ្តល់​implementation របស់interface។ Interface មិនអាចផ្តល់​implementation របស់ abstract class បានលទ។ 6) The abstract keyword គឺលស្របើល ើមបីស្របកាស់ abstract class។ The Interface keyword គឺលស្របើល ើមបីស្របកាស់ Interface។ 7) Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); } ភាេខសគ្នា វាង​Abstract class & Interface:
  • 27.
    27 Polymorphism  Polymorphism មកេីភាសលស្រកក​េីរពាកយគឺpoly និង morphs ។  Poly មានន័យថា​លស្រចើន និង morphs មានន័យថា ទាំរង់ ។  polymorphism មានេីរស្របលេទលៅកន ុង java គឺ 1) compile time polymorphism(static polymorphism) 2) runtime polymorphism (Dynamic polymorphism ) ល ើអ្វីលៅជា Polymorphism?  លយើងអាចអ្នវ ត polymorphism លៅកន ុង java ល យ method overloading និង method overriding ។
  • 28.
    28 Polymorphism ល ើអ្វីលៅជា RuntimePolymorphism ?  Method overriding គឺជាឧទហរណ៍ លអ ចាំលពាោះ runtime polymorphism ។ Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();  លាោះលយើងលៅលមើលឧទហរណ៍ល ើមបីយល់កាន់ចាស់ public class X { public void methodA() //Base class method { System.out.println ("hello, I'm methodA of class X"); } } public class Y extends X { public void methodA() //Derived Class method { System.out.println ("hello, I'm methodA of class Y"); } } public class Z { public static void main (String args []) { X obj1 = new X(); // Reference and object X X obj2 = new Y(); // X reference but Y object obj1.methodA(); obj2.methodA(); } } Output: hello, I'm methodA of class X hello, I'm methodA of class Y
  • 29.
    29 Polymorphism ល ើអ្វីលៅជា Compiletime Polymorphism ?  Method overloading គឺជាឧទហរណ៍ លអ ចាំលពាោះ compile time polymorphism ។ Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();  លាោះលយើងលៅលមើលឧទហរណ៍ល ើមបីយល់កាន់ចាស់ class X { void methodA(int num) { System.out.println ("methodA:" + num); } void methodA(int num1, int num2) { System.out.println ("methodA:" + num1 + "," + num2); } double methodA(double num) { System.out.println("methodA:" + num); return num; } } class Y { public static void main (String args []) { X Obj = new X(); double result; Obj.methodA(20); Obj.methodA(20, 30); result = Obj.methodA(5.5); System.out.println("Answer is:" + result); } } Output: methodA:20 methodA:20,30 methodA:5.5 Answer is:5.5
  • 30.
    30 Polymorphism  អ្នញ្ហញ ិឲ្យinterface មួយអាចស្រ ូវបានលស្របើស្របាស់ជាទូលៅលៅកន ុង class។  ស្រប ិប តិការមួយអាចមាន behavior ខសគ្នន លៅកន ុងករណីលផ្សងគ្នន ។ ល ើ Polymorphism ល្វើការយ៉ា ង ូចលមតច?  Behavior េរងតផ្អកលៅលលើស្របលេទនន data ត លបានលស្របើលៅកន ុងលេលអ្នវ ត។  វល ើរ ួ ទីយ៉ា ងសាំខាន់លៅកន ុងការអ្នញ្ហញ ិឲ្យ object ត លមាន internal structures ល ើមបីតចករំតលក external interface ។  Polymorphism ស្រ ូវបានលគលស្របើយ៉ា ងទូលាំទូោយកន ុងការ implement inheritance ។
  • 31.
    31 Polymorphism ល ើអ្វីជា​Method Overloading?  Method Overloading មានល្ម ោះ ូចគ្នន ​ត ខសគ្នន លៅចាំនួន និង data type នន parameter ។ class Overload { void demo (int a) { System.out.println ("a: " + a); } void demo (int a,int b){ System.out.println ("a and b: " + a + "," + b); } double demo(double a) { System.out.println("double a: " + a); return a*a; } } class MethodOverloading { public static void main (String args []) { Overload Obj = new Overload(); double result; Obj .demo(10); Obj .demo(10, 20); result = Obj .demo(5.5); System.out.println("O/P : " + result); } } Output: a: 10 a and b: 10,20 double a: 5.5 O/P : 30.25
  • 32.
    32 Polymorphism ល ើ MethodOverloading ល្វើការយ៉ា ង ូចលមតច ? 1. Method Overloading អាចល្វើការលៅកន ុង class ូចគ្នន ឬ លៅកន ុង sub-class របស់វ ។ 2. Constructor លៅកន ុង Java អាចជា Overloaded 3. Overloaded methods គួរត មាន argument list ខសគ្នន 4. Parameter អាចខសគ្នន លៅលលើ type ឬ number ឬក ទាំងេីរត មតង ។ 5. េួកវអាចមាន return types ខសគ្នន ឬ ូចគ្នន ។ 6. វកស្រ ូវបានលគលៅថាជា compile time polymorphism ។
  • 33.
    33 Polymorphism ល ើអ្វីជា​Method Overriding?  Method Overriding មានល្ម ោះ ូចគ្នន ​ចាំនួន និង data type នន parameter ក ូចគ្នន ។ public class BaseClass { public void methodToOverride() //Base class method { System.out.println ("I'm the method of BaseClass"); } } public class DerivedClass extends BaseClass { public void methodToOverride() //Derived Class method { System.out.println ("I'm the method of DerivedClass"); } } public class TestMethod { public static void main (String args []) { // BaseClass reference and object BaseClass obj1 = new BaseClass(); // BaseClass reference but DerivedClass object BaseClass obj2 = new DerivedClass(); // Calls the method from BaseClass class obj1.methodToOverride(); //Calls the method from DerivedClass class obj2.methodToOverride(); } } Output: I'm the method of BaseClass I'm the method of DerivedClass
  • 34.
    34 Polymorphism ល ើ MethodOverriding ល្វើការយ៉ា ង ូចលមតច ? 1. អ្នញ្ហញ ិ ត ចាំលពាោះ inherited methods 2. Object type (មិនតមនជា reference variable type)ល យកាំណ ់ overridden method និងស្រ ូវបានលស្របើលៅលេល runtime 3. Overriding method អាចមាន return type ខសគ្នន 4. Overriding method មិនស្រ ូវមាន restrictive access modifier ។ 5. Abstract methods ស្រ ូវបានអាច overridden 6. Static និង final method មិនអាច overridden 7. វស្រ ូវបានលគសា ល់ថាជា Runtime polymorphism ។
  • 35.
  • 36.