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
- 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.
- 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).
- 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.
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:
- Position yourself in the relevant class/method.
- Press F4. This should display the Type Hierarchy view.
- Select the method you’re interested in from the list of methods to the right/bottom.
- 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
- Jump to the start and end of methods, loops, blocks and XML tags
- Get warnings about unused method arguments from Eclipse to keep code clean
- The easiest ways to navigate methods in a class using Eclipse keyboard shortcuts
- Instantly show a class/file in the Package/Project Explorer in Eclipse
- Quick ways to navigate Eclipse editors using the keyboard