Method overriding hoạt động nhờ tính năng run-time method binding trong Java. Vì vậy, nếu chúng ta ép compiler làm static binding cho một method thì có thể ngăn chặn method đó bị override trong derived class.
Chúng ta có thể ngăn chặn method overriding trong Java bằng 3 cách
- Bằng cách làm method final trong base class
- Bằng cách làm method static trong base class
- Bằng cách làm method private trong base class
Final methods không thể bị overridden
Bằng cách làm method final, chúng ta thêm restriction rằng derived class không thể override method cụ thể này.
Ví dụ
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();
}
}Kết quả
Base class test() method Derived class show() method
Static methods không thể bị overridden
Chúng ta không thể override static methods trong derived class vì static methods liên kết với class, không phải với object. Nghĩa là khi gọi static method thì JVM không truyền this reference cho nó như làm cho non-static methods. Do đó run-time binding không thể diễn ra cho static methods.
Ví dụ
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();
}
}Kết quả
Base class test() method Derived class show() method
Private methods không thể bị overridden
Private methods của base class không hiển thị trong derived class, do đó chúng không thể bị override.
Ví dụ
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();
}
}Kết quả
Derived class show() method