Method overriding ทำงานได้เพราะ run-time method binding feature ใน Java ดังนั้น หากเราบังคับให้ java compiler ทำ static binding สำหรับเมธอด เราก็สามารถป้องกันไม่ให้เมธอดนั้นถูก override ใน derived class ได้
เราสามารถป้องกัน method overriding ใน Java ได้ 3 วิธี:
- ทำให้เมธอดเป็น final ใน base class
- ทำให้เมธอดเป็น static ใน base class
- ทำให้เมธอดเป็น private ใน base class
Final methods ไม่สามารถ override ได้
การทำให้เมธอดเป็น final คือการเพิ่มข้อจำกัดว่า derived class ไม่สามารถ override เมธอดนั้นได้
ตัวอย่าง
class Base {
public void show() {
System.out.println("Base class show() method");
}
public final void test() {
System.out.println("Base class test() method");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived class show() method");
}
// can not override test() method because its final in Base class
/*
* public void test() { System.out.println("Derived class test() method"); }
*/
}
public class Test {
public static void main(String[] args) {
Base ref = new Derived();
// Calling the final method test()
ref.test();
// Calling the overridden method show()
ref.show();
}
}
ผลลัพธ์
Base class test() method Derived class show() method
Static methods ไม่สามารถ override ได้
เราไม่สามารถ override static methods ใน derived class ได้ เพราะ static methods ผูกกับ class ไม่ใช่ object หมายถึงเมื่อเราเรียก static method JVM ไม่ส่ง this reference ให้เหมือน non-static methods ทั้งหมด ดังนั้น run-time binding ไม่สามารถเกิดขึ้นกับ static methods ได้
ตัวอย่าง
class Base {
public void show() {
System.out.println("Base class show() method");
}
public static void test() {
System.out.println("Base class test() method");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived class show() method");
}
// This is not an overridden method, this will be considered as new method in Derived class
public static void test() {
System.out.println("Derived class test() method");
}
}
public class Test {
public static void main(String[] args) {
Base ref = new Derived();
// It will call the Base class's test() because it had static binding
ref.test();
// Calling the overridden method show()
ref.show();
}
}
ผลลัพธ์
Base class test() method Derived class show() method
Private methods ไม่สามารถ override ได้
Private methods ของ base class ไม่อยู่ในมุมมอง (not visible) ใน derived class ดังนั้นจึงไม่สามารถ override ได้
ตัวอย่าง
class Base {
public void show() {
System.out.println("Base class show() method");
}
private void test() {
System.out.println("Base class test() method");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived class show() method");
}
// This is not an overridden method, this will be considered as other method.
public void test() {
System.out.println("Derived class test() method");
}
}
public class Test {
public static void main(String[] args) {
Base ref = new Derived();
// Cannot call the private method test(), this line will give compile time error
// ref.test();
// Calling the overridden method show()
ref.show();
}
}
ผลลัพธ์
Derived class show() method