មេរៀនទី២០: Static Dynamic Binding

ការភ្ជាប់ method call ទៅ method body ដូចជា binding។
មានពីរប្រភេទនៃ binding
1.    static binding
2.    dynamic binding
1) បណ្តាអថេរ មាន type មួយ
1.    int data=30;
Here data variable is a type of int.
2) References មាន type មួយ
class Dog{
public static void main(String args[]){
Dog d1;//Here d1 is a type of Dog
}
}
3) Objects មាន type មួយ
An object is an instance of particular java class,but it is also an instance of its superclass.
class Animal{}

class Dog extends Animal{
public static void main(String args[]){
Dog d1=new Dog();
}
}

static binding
ឧទាហរណ៍ static binding
class Dog{
private void eat(){System.out.println(“dog is eating…”);}

public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}

Dynamic binding
កាលណា object ត្រូវបានកំណត់ពេលរត់ run-time, វាគឺជា dynamic binding។
ឧទាហរណ៍  dynamic binding
class Animal{
void eat(){System.out.println(“animal is eating…”);}
}

class Dog extends Animal{
void eat(){System.out.println(“dog is eating…”);}

public static void main(String args[]){
Animal a=new Dog();
a.eat();
}
}
Output:dog is eating…