Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly determine if a method is overridden in Java

Tags:

There is a possible optimization I could apply to one of my methods, if I can determine that another method in the same class is not overridden. It is only a slight optimization, so reflection is out of the question. Should I just make a protected method that returns whether or not the method in question is overridden, such that a subclass can make it return true?

like image 906
bgw Avatar asked Feb 23 '10 00:02

bgw


People also ask

How do you know if Java is overriding?

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

What determines method overriding?

Method overriding is one of the way by which java achieve Run Time Polymorphism. The version of a method that is executed will be determined by the object that is used to invoke it.

When we say method is overridden?

Conclusion. In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.

Which method is overridden in Java?

Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden.


3 Answers

I wouldn't do this. It violates encapsulation and changes the contract of what your class is supposed to do without implementers knowing about it.

If you must do it, though, the best way is to invoke

class.getMethod("myMethod").getDeclaringClass(); 

If the class that's returned is your own, then it's not overridden; if it's something else, that subclass has overridden it. Yes, this is reflection, but it's still pretty cheap.

I do like your protected-method approach, though. That would look something like this:

public class ExpensiveStrategy {   public void expensiveMethod() {     // ...     if (employOptimization()) {       // take a shortcut     }   }    protected boolean employOptimization() {     return false;   } }  public class TargetedStrategy extends ExpensiveStrategy {   @Override   protected boolean employOptimization() {     return true; // Now we can shortcut ExpensiveStrategy.   } } 
like image 166
John Feminella Avatar answered Oct 07 '22 10:10

John Feminella


Well, my optimization is a small yield on a case-by-case basis, and it only speeds things a lot because it is called hundreds of times per second.

You might want to see just what the Java optimizer can do. Your hand-coded optimization might not be necessary.

If you decide that hand-coded optimization is necessary, the protected method approach you described is not a good idea because it exposes the details of your implementation.

like image 23
jdigital Avatar answered Oct 07 '22 09:10

jdigital


How many times do you expect the function to be called during the lifetime of the program? Reflection for a specific single method should not be too bad. If it is not worth that much time over the lifetime of the program my recommendation is to keep it simple, and don't include the small optimization.

Jacob

like image 25
TheJacobTaylor Avatar answered Oct 07 '22 11:10

TheJacobTaylor