Polymorphism
Polymorphism (ពហុរូបភាព) ក្នុង Java
🎭 Polymorphism គឺជាអ្វី?
Polymorphism មកពី Greek ពាក្យ "poly" (ច្រើន) + "morph" (រូបភាព) = ច្រើនរូបភាព។ វាអនុញ្ញាតឱ្យ objects ខុសៗគ្នាឆ្លើយតបតាមវិធីផ្សេងគ្នា ទៅនឹង method call ដូចគ្នា។
ឧទាហរណ៍ក្នុងជីវិតពិត:
- ម៉ូតូ: ម៉ូតូគ្រប់ប្រភេទមាន "start()" ប៉ុន្តែ Honda start ខុសពី Yamaha
- សត្វ: ទាំងអស់មាន "makeSound()" - ឆ្កែ bark, ឆ្មា meow, សេះ neigh
- គណិតវិទ្យា: + អាច បូក លេខ ឬ concatenate Strings
📚 ប្រភេទ Polymorphism
ប្រភេទ | ពេល | របៀប | ឧទាហរណ៍ |
---|---|---|---|
Compile-time | Compile time | Method Overloading | add(int, int), add(double, double) |
Runtime | Runtime | Method Overriding | Animal a = new Dog(); a.sound(); |
⚡ Compile-time Polymorphism (Static)
កំណត់ត្រូវនៅពេល compile តាមរយៈ Method Overloading។
class Calculator {
// Same method name, different parameters
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Calculator calc = new Calculator();
calc.add(5, 10); // ហៅ method 1
calc.add(5.5, 10.5); // ហៅ method 2
calc.add(5, 10, 15); // ហៅ method 3
🚀 Runtime Polymorphism (Dynamic)
កំណត់ត្រូវនៅពេល runtime តាមរយៈ Method Overriding & Inheritance។
class Animal {
void makeSound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Cat meows");
}
}
// Polymorphism in action!
Animal myAnimal;
myAnimal = new Dog();
myAnimal.makeSound(); // "Dog barks" (Runtime decision)
myAnimal = new Cat();
myAnimal.makeSound(); // "Cat meows" (Runtime decision)
🔑 ចំណុចសំខាន់
- Reference type: Animal (Parent)
- Object type: Dog, Cat (Child)
- Method called: កំណត់តាម Object type (ពេល runtime)
- "One interface, many implementations"
Java Code
Click "Run" to execute the Java code
🎯 Upcasting & Downcasting
⬆️ Upcasting (Implicit)
Child object → Parent reference (ស្វ័យប្រវត្តិ, safe)
class Animal { }
class Dog extends Animal { }
Dog dog = new Dog();
Animal animal = dog; // Upcasting (ស្វ័យប្រវត្តិ)
⬇️ Downcasting (Explicit)
Parent reference → Child type (ត្រូវ cast, មានហានិភ័យ)
Animal animal = new Dog(); // Upcasting
Dog dog = (Dog) animal; // Downcasting (ត្រូវ cast)
// ប្រើ instanceof ដើម្បីសុវត្ថិភាព
if (animal instanceof Dog) {
Dog myDog = (Dog) animal;
myDog.bark(); // Safe!
}
📊 Upcasting vs Downcasting
លក្ខណៈ | Upcasting | Downcasting |
---|---|---|
ទិស | Child → Parent | Parent → Child |
ស្វ័យប្រវត្តិ | ✅ Yes | ❌ No (ត្រូវ explicit cast) |
សុវត្ថិភាព | ✅ Safe | ⚠️ អាច ClassCastException |
ការប្រើ | Polymorphism | ចូលប្រើ child methods |
🛡️ instanceof Operator
ពិនិត្យថា object គឺជា instance របស់ class ណា។
Animal animal = new Dog();
if (animal instanceof Dog) {
System.out.println("This is a Dog");
Dog dog = (Dog) animal; // Safe downcasting
}
if (animal instanceof Cat) {
System.out.println("This is a Cat"); // មិន print
}
Java Code
Click "Run" to execute the Java code
💡 Polymorphism Benefits & Use Cases
✅ ប្រយោជន៍
- Code Reusability: Method តែមួយ objects ច្រើន
- Flexibility: ងាយប្តូរ implementation
- Maintainability: កូដងាយ maintain
- Extensibility: បន្ថែម classes ថ្មីដោយមិនកែកូដចាស់
- Loose Coupling: កាត់បន្ថយ dependency
🎯 Real-World Use Cases
1. Payment Processing
class Payment {
void processPayment(double amount) {
System.out.println("Processing payment...");
}
}
class CreditCardPayment extends Payment {
@Override
void processPayment(double amount) {
System.out.println("Processing credit card: $" + amount);
}
}
class PayPalPayment extends Payment {
@Override
void processPayment(double amount) {
System.out.println("Processing PayPal: $" + amount);
}
}
class BankTransfer extends Payment {
@Override
void processPayment(double amount) {
System.out.println("Processing bank transfer: $" + amount);
}
}
// ប្រើ polymorphism
void checkout(Payment payment, double amount) {
payment.processPayment(amount); // Works with any payment type!
}
checkout(new CreditCardPayment(), 100);
checkout(new PayPalPayment(), 50);
checkout(new BankTransfer(), 200);
2. Shape Hierarchy
abstract class Shape {
abstract double getArea();
abstract double getPerimeter();
}
class Circle extends Shape {
double radius;
Circle(double r) { this.radius = r; }
double getArea() {
return Math.PI * radius * radius;
}
double getPerimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
double length, width;
Rectangle(double l, double w) {
this.length = l;
this.width = w;
}
double getArea() {
return length * width;
}
double getPerimeter() {
return 2 * (length + width);
}
}
// Polymorphic method
void printShapeInfo(Shape shape) {
System.out.println("Area: " + shape.getArea());
System.out.println("Perimeter: " + shape.getPerimeter());
}
printShapeInfo(new Circle(5));
printShapeInfo(new Rectangle(10, 5));
⚠️ Polymorphism Limitations
- Only overridden methods: Polymorphism works តែជាមួយ methods ដែល override
- Cannot access child-specific: Parent reference មិនអាចចូលប្រើ child's unique members
- Not for static methods: Static methods មិនមាន polymorphism
- Not for variables: Variable hiding មិនមែន polymorphism
💡 Best Practices
- Program to interface: ប្រើ parent type reference
- Use instanceof carefully: ជៀសវាងការប្រើច្រើន
- Override equals() & hashCode(): នៅពេលប្រើ Collections
- @Override annotation: ប្រើជានិច្ច
- Favor composition: HAS-A ជាងពេក IS-A
Java Code
Click "Run" to execute the Java code