[英]Class that implements an interface in a jar, but does not override the methods
My project has a dependency to apache commons-dbcp and we are using version 1.2.2 我的项目对apache commons-dbcp有依赖性,我们正在使用1.2.2版
The commons-dbcp version we are using has the class org.apache.commons.dbcp.BasicDataSource
which implements javax.sql.DataSource
which in turn implements java.sql.Wrapper
, however BasicDataSource
does not implement the methods defined in Wrapper
(jdk 1.6.x). 我们正在使用的commons-dbcp版本具有org.apache.commons.dbcp.BasicDataSource
类,该类实现javax.sql.DataSource
,该类又实现了java.sql.Wrapper
,但是BasicDataSource
不实现Wrapper
定义的方法(jdk 1.6 。X)。 But my project which has a class ( MyClass
) that extends BasicDataSource
compiles without any problems - I am a bit puzzled here. 但是我的项目有一个扩展了BasicDataSource
的类( MyClass
),编译时没有任何问题-我有点困惑。 In IntelliJ, I see MyClass
as an error though. 在IntelliJ中,我将MyClass
视为错误。
That class has been compiled with an earlier version of the javax.sql.DataSource
interface (the Java 5 or earlier version). 该类已使用javax.sql.DataSource
接口的早期版本(Java 5或更早版本)进行编译。 The Wrapper
interface was introduced with Java 6 / JDBC 4.0. Wrapper
接口是Java 6 / JDBC 4.0引入的。
When the classloader loads a class that implements an interface, but doesn't implement all the new methods, it allows it. 当类加载器加载实现接口但未实现所有新方法的类时,它会允许它。 When the method is actually called, a NoSuchMethodError
is thrown (I believe it stubs the missing methods to throw this exception). 当实际调用该方法时,会引发NoSuchMethodError
(我相信它会对丢失的方法进行存根处理以引发此异常)。 This allows for - limited - forward compatibility: as long as that method isn't actually called the implementation will work. 这允许-有限-前向兼容性:只要实际上不调用该方法,该实现就可以工作。
This also means that when you subclass this class it will behave as if all methods have been implemented (even if they weren't), as long as the source of that class isn't part of your project. 这也意味着,当您对该类进行子类化时,只要该类的源不属于您的项目,它的行为就好像所有方法都已实现(即使尚未实现)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.