Considering some security issues, I don't want the subclass inherit the static method from its super class,not even call this method, how can I do this? Please help!
Static methods are not inherited in the same sense as instance methods are. If you declare a static method as public (or package private) it is accessible whether or not there is a local redeclaration in a child class. The local redeclaration merely means that the child class has to qualify the name of the method; e.g.
public class Parent {
public static void foo() { ... }
}
public class Child {
public static void foo() { ... }
public static void main(String[] args) {
foo(); // calls local override
Parent.foo(); // calls original version.
}
If you don't want the Child class to be able to call the Parent.foo method, then you need to declare it as private (or maybe package private if Child and Parent are in different packages.)
But even then, if the Child class has permission to use reflection, then it can easily use that to call a private method in the Parent class. So unless you are sandboxing your code Java access modifiers are not a security mechanism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With