Use methods declared in implementation that are not defined in interface

后端 未结 6 1797
不知归路
不知归路 2020-12-18 11:56

I have a class defined by an interface

public interface Test {
    void testMethod();
}

Test test = new TestImpl();

public class TestImpl implements Test {         


        
6条回答
  •  梦毁少年i
    2020-12-18 12:39

    You can call it if you cast to the implementing class, the one that implements that method In short:

    Test test = new TestImpl();
    
    // ... and later / somewhere else
    
    ((TestImpl) test).anotherMethod();
    

提交回复
热议问题