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();
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 មួយប៉ាល្ណ ោះ ។
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");}}
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
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