Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine if a method has been overridden in a Java class

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);     }  } 
like image 237
peterk Avatar asked Jul 24 '16 17:07

peterk


People also ask

How do you check if a method is overridden?

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.

When a class has overridden a method it has?

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.

Which methods are overridden in Java?

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.

Are overridden methods inherited?

Overridden methods are not inherited.

What is method overriding in Java?

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

How to tell if a subclass has overridden a method?

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:

What is the difference between method overloading and method overriding?

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.

Can we access the method of the superclass after overriding in Java?

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.


1 Answers

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.

like image 113
Sergey Kalinichenko Avatar answered Oct 11 '22 21:10

Sergey Kalinichenko