简体   繁体   中英

Override/Change final class method implementing interface java

Java - I have an Interface say InterfaceA that has three methods - method1, method2, method3.

A final class implements this interface. This class and interface are in a Jar and their implementations are unknown.

The Method method2 of class is not working as per expectations. What are possible ways in which i can have the right implementation/override this method.

In Java you cannot extend final classes.


So the only way is to code your own class with

  • your own the method implementation
  • call the final class methods in other cases

class Third implements TheInterfaceInQuestion {

    FinalClass fc;

    public Third() {
       fc = new FinalClass();
    }

    @Override
    public void method1() {
        // call final class implementation
        fc.method1();
    }

    @Override
    public void method2() {
        // new implementation
    }

    @Override
    public void method3() {
        // call final class implementation
        fc.method3();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM