I want to be able to determine if a base class method has been overridden by a subclass specifically because expensive setup is needed before invoking it and most subclasses in our system do not override it. Can it be tested by using reflection provided method handles? Or is there some other way to test if a class method is overridden?
e.g.
class BaseClass { void aMethod() { // don nothing } protected boolean aMethodHasBeenOverridden() { return( // determine if aMethod has been overridden by a subclass); } }
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.
The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.
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.
Overridden methods are not inherited.
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. Usage of Java Method Overriding
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:
Method overloading vs. method 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.
A common question that arises while performing overriding in Java is: Can we access the method of the superclass after overriding? Well, the answer is Yes. To access the method of the superclass from the subclass, we use the super keyword. I am an animal. I am a dog.
You can do it with reflection by examining the declaring class of your method:
class Base { public void foo() {} public void bar() {} } class Derived extends Base { @Override public void bar() {} } ... Method mfoo = Derived.class.getMethod("foo"); boolean ovrFoo = mfoo.getDeclaringClass() != Base.class; Method mbar = Derived.class.getMethod("bar"); boolean ovrBar = mbar.getDeclaringClass() != Base.class; System.out.println("Have override for foo: "+ovrFoo); System.out.println("Have override for bar: "+ovrBar);
Prints
Have override for foo: false Have override for bar: true
Demo.
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