Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overiding same method in Super Class and Interface

I am extending a Class as well as implementing an interface, both contain a method called doFilter , it is a final method in my super class and off course an abstract method from my interface . So, now I am getting compiler error.

How should i resolve this.

My filter class is :

public class MyCacheFilter extends CachingFilter implements Filter {
    // here is error, as compiler is thinking I am overriding dofilter of
    // CacheFilter, although I have not specified any @Override annotation

    public doFilter() {
    }
}
like image 701
Chandan Avatar asked Dec 09 '25 14:12

Chandan


2 Answers

If the method is provided by CachingFilter you don't have to provide an implementation in MyCacheFilter. (In fact, as you've discovered, you can't.)

If you really want a different behavior of doFilter than what is provided by CachingFilter then inheritance is not an option. In that case, consider using composition:

public class MyCacheFilter implements Filter {

    CachingFilter cachingFilter = ...

    @Override
    public void doFilter() {
        ...
        possibly refer to cachingFilter...
        ...
    }

    public String otherMethod() {
        // delegate to cachingFilter
        return cachingFilter.otherMethod();
    }
}

[...] although I have not specified any @Override annotation

Whether or not you explicitly provided @Override doesn't make a difference.

like image 75
aioobe Avatar answered Dec 12 '25 02:12

aioobe


The problem is that the method is declared final in the superclass. That means, literally, "this method can't be overridden". If you need to put your own logic in doFilter(), you can't extend the CachingFilter.

There is no concept in Java of where you override a method from. The interface is the contract which tells others what your class can do. The complete contract is the sum of all interfaces implemented. It doesn't matter if an identical method signature is present in more than one interface. The class is the implementation of the contract.

Inheritance starts from the top (ie Object) and each overridden method replaces any pre-existing definitions from any parent as the publicly exposed method - except if a method is declared final. That tells the compiler that this method implementation is not allowed to be overridden, this is the final version of it.

EDIT: I don't know what the CachingFilter you are using is coming from, but a common pattern in frameworks layered on top of other frameworks or java standard API is to create a final implementation of the API-required method (doFilter() in this case), but add another "internal" method (like internalDoFilter()) that gets called from the doFilter(). Extending classes can then safely override the "internal" method while still guaranteeing that any essential logic in doFilter() is still executed properly.

like image 21
pap Avatar answered Dec 12 '25 02:12

pap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!