I know I can use reflection to invoke a private method, and to get or set the value of a private variable, but I want to override a method.
public class SuperClass {
public void printInt() {
System.out.println("I am " + getClass() + ". The int is " + getInt());
}
private int getInt() {
return 1;
}
}
public class SubClass extends SuperClass {
public static void main(String[] args) {
(new SubClass()).printInt();
}
public int getInt() {
return 2;
}
}
I want the main
method in SubClass
to print out 2
, but it prints out 1
.
I've heard this can be done through reflection, but I can't figure out how.
If not reflection, does anyone know of another way of doing it?
(Other than making SuperClass.getInt()
protected, or copying and pasting the printInt()
method into SubClass
.)
If actually overriding the private method is not possible, is there a way of placing some sort of trigger on it that will invoke a method in my sub-class either before or after the private method executes?
No, we cannot override private or static methods in Java. Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.
Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier.
You can't override a private method because no other class, including a derived class, can tell that it exists. It's private.
Private methods are implicitly final
.
On a related note, a subclass can declare a field or method with the same name as a private
field or method in a super class, because from the subclass's point of view, these members do not exist. There's no special relationship between these members.
Private methods are not inherited and cannot be overridden in any way. Whoever told you you can do it with reflection was either lying or talking about something else.
However, you can access the private method getInt
of whatever subclass is invoking printInt
like so:
public void printInt() throws Exception {
Class<? extends SuperClass> clazz = getClass();
System.out.println("I am " + clazz + ". The int is " +
clazz.getMethod("getInt").invoke(this) );
}
This will have the effect of the subclass' getInt
method being called from the superclass' printInt
.
Of course, now this will fail if the subclass doesn't declare a getInt
, so you have to add a check to be able to handle "normal" subclasses that don't try to "override" a private method:
public void printInt() throws Exception {
Class<? extends SuperClass> clazz = getClass();
// Use superclass method by default
Method theGetInt = SuperClass.class.getDeclaredMethod("getInt");
// Look for a subclass method
Class<?> classWithGetInt = clazz;
OUTER: while( classWithGetInt != SuperClass.class ){
for( Method method : classWithGetInt.getDeclaredMethods() )
if( method.getName().equals("getInt") && method.getParameterTypes().length == 0 ){
theGetInt = method;
break OUTER;
}
// Check superclass if not found
classWithGetInt = classWithGetInt.getSuperclass();
}
System.out.println("I am " + classWithGetInt + ". The int is " + theGetInt.invoke(this) );
}
You still have to change superclass code to make this work, and since you have to change superclass code, you should just change the access modifier on getInt
to protected
instead of doing reflection hack-arounds.
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