开发者

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 MyCacheFilte开发者_C百科r 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() {
    }
}


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.


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.


Unfortunately there is no way to resolve such conflicts directly, other than by rethinking your class design. You could

  • rename the method in the interface, or
  • change MyCacheFilter to contain a CachingFilter rather than inherit from it, or
  • if the superclass implementation suits your needs, you can (as @aioobe mentioned) leave the method out of your subclass altogether.


If CachingFilter.dofilter() is available, it will be a method in your child class. So you don't need to override it. If you want to override, method shouldn't be final.


Try implementing the interface as anonymous.

public class MyClass extends MySuperClass implements MyInterface{

MyInterface myInterface = new MyInterface(){

/* Overrided method from interface */
@override
public void method1(){

}

};

/* Overrided method from superclass*/
@override
public void method1(){

}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜