Take the 2-minute tour ×

I'm now focusing on a project requiring insight of Java byte code.

With the help of bcel, I can now complete most of the work. One point that I'm now not clear is how to identify a sub-class method override its base code? Is there any attribute recorded in the .class file associated with a method indicating this overriding relationship or should I go backwards to its base class can compare method signatures?

Any hints will be highly appreciated.

share|improve this question

4 Answers 4

up vote 4 down vote accepted

You need to look up the hierarchy chain--there's nothing in the byte code that indicates it's an overridden method, because there doesn't need to be.

share|improve this answer
Why it's not necessary? Then how JVM link different .class file dynamically? Does it search the inherent hierarchy bottom up, until it found a method that matches? Thanks. –  Summer_More_More_Tea Dec 1 '11 at 15:34
@Summer_More_More_Tea Flip the question--why would it be necessary? If it's a method of the class you're looking at, why would the VM need to know it's overridden? –  Dave Newton Dec 1 '11 at 15:59
@DAVE sounds ground.:) Thanks. –  Summer_More_More_Tea Dec 1 '11 at 16:21

If you can't rely on the @Override attribute then it seems that according to the spec there is no other way to know by looking just at the class. I think you need to look at the superclasses.

share|improve this answer
@Override has SOURCE retention. –  Dave Newton Dec 1 '11 at 15:31
@DᴀᴠᴇNᴇᴡᴛᴏɴ: Thanks. –  opqdonut Dec 1 '11 at 15:32
@DAVE +1. also thanks opqdonut for the reply. –  Summer_More_More_Tea Dec 1 '11 at 15:36

Unfortunately, you can't tell that from the bytecode. The annotation @Override is only an advisory one - it's not mandatory.

The JVM defines 5 ways of invoking a method. They are invokevirtual, invokeinterface, invokespecial, invokestatic and the new invokedynamic.

Focus on invokevirtual - it's the most common form of dispatch and is the one used for the case you're talking about here.

The way that invokevirtual works is that at runtime it looks at the class of the object you're dispatching on. If it finds an implementation of the method we're after, then it calls it. If not, then it looks at the superclass of the object's class and tries again, and so on.

So there is no way from the bytecode to reliably tell whether a given method is overridden, without looking at the bytecode for the parent class.

share|improve this answer

You could decompile it and load the code as a project in the IDE of you choice. Normally you can easily jump to overridden methods from the inheriting class.

share|improve this answer
Thanks for your fast response. But how to achieve this with Java code? :) –  Summer_More_More_Tea Dec 1 '11 at 15:25

Your Answer


