Let's say you you have a class called Vehicle that outlines a protected (could also be abstract) method called speed. And, the sub class Car overrides that method to define its own implementation.

Now, assume that the need to have a speed method in the Vehicle class is no longer required (let's say all vehicles will be stationary for the whatever reason).

If I remove the method speed from Vehicle, I would like to throw a compile error so that so that the developer who has removed the method knows that sub class(es) are potentially relying on it to perform certain actions.

Technically speaking a compile error is not needed, but some sort of notification that acts as a hurdle when such re factoring is happening. Is there a programming pattern that can be employed to handle such a situation?

UPDATE: I am using Java 1.4 (Sorry!)

4

There are 4 answers

2
Dave Newton On

The @Override annotation is expressly for this purpose.

If you're not using Java 1.5+, then no, although you could use AOP to instrument those methods to throw an exception, or just use reflection and classpath scanning to go over all subclasses and check for the presence of said method.

2
npe On

Use @Override annotation for methods in subclases. Once you remove the method from a base-class, tools like Eclipse and javac will issue a warining for those no-longer-overriding methods.

Edit: While you cannot use @Override before Java 1.5.0, there is a tool called xdoclet. Back in the days of J2EE and EJB 2.1 this was used to "simulate" annotations and do magical things with code based on javadoc-like markers. Look at it, maybe you can use it.

Edit 2: In Java 1.4.x, you can also use JavaDoc tag {@inheritDoc} for this kind of verification. Instead of annotating your method with @Override annotate it with @inheritDoc, like this:

public class MyAwesomeClass extends BaseClass
{
    /** {@inheritDoc} */
    protected void myAweSomeMethod()
    {
        //...
    }
}

Now, if you change the myAweSomeMethod signature in BaseClass, or remove it, you will get warnings from JavaDoc tool, similar to this:

/home/npe/java-tests/MyAwesomeClass.java:4: warning - @inheritDoc used but myAwesomeMethod does not override or implement any method.

0
alaster On

You can write super.speed() in nested classes and leave this method empty in parent. If you delete now this method in parent, you'll have an Exception. But there is a disadvantage - you must call it from all overrided methods. Try it, perhaps this will help you

0
Thomas On

If it's abstract, then there is no implementation that can be removed from the parent class, and your risk comes down to new subclasses not implementing it. If it's protected and defined in the parent, there are two cases that should already throw compiler errors if the parent implementation is removed.

1) A subclass calls that method without defining its own implementation. Method does not exist.

2) A subclass defines the method, but includes a call to super. Again, the method does not exist.