Find all classes that override/reimplement a method

Inheritance is a great feature of OO. It can make browsing and navigating code a lot more difficult though, especially in a class hierarchy where some classes override a method along the hierarchy but others don’t.

Here’s an example. Your task is to find all the classes that implement/override the method connect().

public interface DataSource {
 public void connect();
 public void update();
 public void disconnect();
}

public abstract class DBDataSource implements DataSource {
 public void connect() {
 // ... Connect to DB
 }

 public void disconnect() {
 // ... Disconnect from DB
 }
}

public class AccountDataSource extends DBDataSource {
 public void update() {
 // ... Update account
 }
}

public class ClientDatasource extends DBDataSource {
 public void connect() {
 // ... Special code to connect to client DB
 }

 public void update() {
 }
}

In this case DBDataSource implements the method, ClientDataSource overrides it but AccountDataSource doesn’t. Finding this out manually is slow and time consuming because the classes are in different files.

There are two lesser known features of Eclipse that can you help. The first one is an unknown feature of the Type Hierarchy called Lock View and Show Members and the second one is Quick Type Hierarchy which is my favourite and which I use all the time.

How to use Quick Type Hierarchy

  1. Position the cursor on the name of the method you’re interested in. You can be in any class on the class hierarchy, not necessarily the parent interface/class.
  2. Press Ctrl+T. A popup will appear with all classes that inherit/override this method. Look for the classes that are not greyed out. They implement/override the method, whereas the greyed out ones don’t (depending on where you are in the hierarchy, the class may not even appear in the list).
  3. Move up/down using the arrow keys and press Enter on one of the classes and you’ll immediately be taken to the overriden method.

Below is an example of what you might see. Notice the greyed out classes – those don’t reimplement the method connect.

Quick Type Hierarchy Example

Tip: You can also use Quick Type Hierarchy to move between the class hierarchy without bringing up the Type Hierarchy view. It’s often a lot faster.

How to use the Type Hierarchy (Lock View)

Most developers are familiar with the Type Hierarchy (F4), but a not-so-familiar feature is the ability to view the hierarchy from the point of view of the members of the class (ie. methods and fields) rather than the class itself.

To see which classes override/reimplement a method, do the following:

  1. Position yourself in the relevant class/method.
  2. Press F4. This should display the Type Hierarchy view.
  3. Select the method you’re interested in from the list of methods to the right/bottom.
  4. Click the Lock View and Show Members button (). You should see all classes in the hierarchy, with the method name above below the classes that override/reimplement the method. The ones that don’t reimplement it will appear, but won’t list the method below them.

This is what you should see (my Type Hierarchy view looks different to the Eclipse default because it’s in Horizontal View).

Tip: Use this feature when you want to browse through all classes at leisure. Use the Quick Type Hierarchy when you only want a quick overview of all classes and to go directly to a specific class and finish.

Related Tips

Support Eclipse On E

Leave a Reply

Your email address will not be published. Required fields are marked *