How to call a base class method without changing all derived classes

Go To StackoverFlow.com

2

I have a class called Base which has a method called execute(). There are about 100 classes which derive from this base class and provide their own implementation of execute(). Now, I have some common logic which I want to put in Base.SomeMethod(). This method needs to be called at the end of execute(). My question whether it is possible to call this without changing each and every derived class's execute() method?

2012-04-05 18:24
by Asha


7

public class Base {
    public final void execute() {
        doExecute();
        someMethod();
    }

    protected abstract void doExecute();

    public void someMethod() {
    }
}

This solution prevents the super code smell.

2012-04-05 18:28
by Jeffrey
I have to change all derived class from execute to doExecute right - Asha 2012-04-05 18:38
@Asha Yes, but most IDEs will let change all of the methods at once so it shouldn't be that big of a deal - Jeffrey 2012-04-05 18:39


2

Yes, but you have to change the callers then. Callers will have to call a doExecute() (find a better name for it though) method, which you define in your base class as final, and which calls execute(), then the common code.

Another option is aspect-oriented programming, but I wouldn't recommend it for this purpose, that is, to "hack" code.

The question is: why is changing the name of a method in a 100 or so classes such a problem? It's a click of the mouse with an IDE.

2012-04-05 18:27
by biziclop
yes that is simple..but I don't everybody who derives from my class in future to remember to call this method - Asha 2012-04-05 18:30
This is not such a terrible idea if you're already using Spring or something like that that allows for semi non-painful proxying. But I agree; I wouldn't go that route for convenience sake only. Bite the bullet and change all the derived classes if possible - Todd Murray 2012-04-05 18:32
They don't have to, as it's the other way around. Take a look at the code posted by @Jeffrey, that's what I meant - biziclop 2012-04-05 18:34


0

Not that I'm aware of. Next time you should consider that you might want to add some common action for all extended classes, and call for super.execute()!

2012-04-05 18:27
by Madara Uchiha


0

Only by using something that instruments your code; this isn't possible with pure Java.

2012-04-05 18:28
by sethcall


0

Let me state your problem as i understand : Animal class has Breath() method which has implementation and due to inheritance all the subclasses has this member and unless there is very different way of breathing nobody will override.

Now at the end of Breath method you want to call CloseEyes() method of animal class and may be that is true that some or all of the subclasses overrides CloseEyes() method.

So your problem : Everytime any animal breath you want to them to CloseEyes but from Animal class and not from the derived classes.

  1. If there are already CloseEyes() methods in many derived classes then you are actually doing something wrong in calling base class's CloseEyes().

  2. If you still want only base class's method to be called then why do you need same method name- you just say AnimalEyeClose() , make it private and have it in Animal class.

2012-04-06 03:17
by Dhananjay
when did I say that I want method with the same name - Asha 2012-04-06 05:18
Ads