简体   繁体   English

在jar中实现接口但不覆盖方法的类

[英]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.

相关问题 编写DAO:如何在抽象类中覆盖或实现接口方法? - Writing DAO : how to override or implements the interface methods in abstract class? 如何在实现接口的类中重写方法 - How to override method in class that implements interface 实现接口的类不起作用,因为“类不是抽象的,并且不会覆盖抽象方法……”。 怎么了 - The class that implements the interface doesn't work because “Class is not abstract and does not override abstract method…”. What is the mistake? 无法从实现接口的类中实现方法? - Unable to implement methods from a class that implements an interface? 我的课程实现了一个接口,但没有覆盖它的抽象方法。 为什么Eclipse仍然可以毫无问题地进行编译? - My class implements an Interface but doesn't override it's abstract methods. Why is Eclipse still compiling it without an issue? 为什么扩展接口的方法在实现扩展接口的类中不可见? - Why are methods of extended interface not visible in class that implements extending interface? 为什么接口方法在实现接口的类中不能是静态的? - Why interface methods can't be static in class that implements the interface? 类在什么条件下实现接口? - In what condition does a class implements an interface? 如何在运行时从jar文件加载实现接口的类? - How to load a class which implements a interface from a jar file at runtime? 如何在实现导入接口的抽象类中调用方法? - How to call methods within my abstract class that implements an imported interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM