views:

83

answers:

6

Hi,

Is there any keyword or design pattern for doing this?

Please check the update

public abstract class Root
{
    public abstract void foo();
}

public abstract class SubClass extends Root
{
    public void foo()
    {
        // Do something
        //---------------- Update -------------------//
        // This method contains important code
        // that is needed when I'm using a instance
        // of SubClass and it is no instance of any
        // other class extending SubClass

    }
}

public class SubberClass extends SubClass
{
    // Here is it not necessary to override foo()
    // So is there a way to make this necessary? 
    // A way to obligate the developer make again the override 
}

Thanks

+3  A: 

If every class subclassing SubClass has to override foo() then why provide an implementation at all in SubClass? You can simply remove the method definition from SubClass and then all subclasses will be forced to provide an implementation.

Adamski
Yes, I know. But I don't want to lose the code in SubClass. I explained in my update.
Martijn Courteaux
+9  A: 

If you are doing this, then you are probably abusing inheritance; inheritance, contrary to popular myth, is not intended for making custom hooks/handlers, but rather to enable alternative implementations.

If you want your user to provide some sort of function/hook/callback, then you should define an interface that provides just those methods that you need your user to define. Then you should require the user to pass in an instance of that interface to your object's constructor or passed into the function that needs it.

Aggregation, delegation, and composition are frequently better and safer design patterns than inheritance; forcing other users to inherit from your class, is incredibly risky, as it provides the user with many opportunities to violate the contract of your class or to invalidate the invariant of your base class.

Michael Aaron Safyan
A: 

Instead of overriding foo() in SubClass, create a new method fooImpl() and leave foo() abstract. This way, all classes must implement foo() but you can simply implement it by calling fooImpl() if that is already enough.

Aaron Digulla
+1  A: 

Yeah it is not necessary to override foo() in SubberClass.

fastcodejava
A: 

If you really want to, you can re-declare foo as abstract.

public abstract class SubberClass extends SubClass
{
    public abstract void foo();
}
ILMTitan
A: 

You can't have it both ways. You can't provide a method with a default implementation AND require child classes override it. Instead of declaring the method as abstract in Root, you could define an interface (IFoo) with the method declared and then provide an abstract class that implements the interface. That would still require a concrete child class but would not require a method override.

Most of the time you see this type of pattern, an interface is used to define a set of methods and an abstract base class provides some default implementations for some but not all methods from the interface. This requires the concrete child class to provide code for the remaining methods and the option to override the default behaviors.

In any case, you can't provide a default behavior for a single method and require child classes to override that same method.

Kelly French