[英]Does the Hashtable implement every method in the Map interface?
Since a class that implements an interface in Java must define every method in the interface to avoid being declared as abstract, I was wondering about the following: 由于用Java实现接口的类必须定义该接口中的每个方法,以避免被声明为抽象,所以我想知道以下内容:
When I create a program that instantiates a Hashtable object, why am I not required to define every method in the Map interface? 创建实例化Hashtable对象的程序时,为什么不需要在Map接口中定义每个方法? Are the methods I do not explicitly define, created automatically as "stubs"?
我没有明确定义的方法会自动创建为“存根”吗?
You are creating an object from Hashtable
class which already implemented all necessary methods.You are not creating a class, but an object from that class. 您正在从
Hashtable
类创建一个对象,该对象已经实现了所有必需的方法。您不是在创建类,而是从该类创建对象。 If you created a class which implements Map
, the compiler would ask you to implement all the necessary methods. 如果您创建了一个实现
Map
的类,则编译器会要求您实现所有必要的方法。
Does the Hashtable implement every method in the Map interface?
Hashtable是否实现Map接口中的所有方法?
Yes. 是。 If you take a look at the JavaDoc the Hashtable class has been defined as
如果您看一下JavaDoc,则将Hashtable类定义为
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, Serializable
Notice, that the class has not been declared abstract and it implements Map . 请注意,这个类没有被声明为抽象的 ,它实现了Map。 Hence, it must and it does implement all the methods defined in
Map
interface. 因此,它必须并且确实实现了
Map
接口中定义的所有方法。
When I create a program that instantiates a Hashtable object, why am I not required to define every method in the Map interface?
创建实例化Hashtable对象的程序时,为什么不需要在Map接口中定义每个方法?
When you instantiate an object, the implementation of its methods is provided by its class. 实例化对象时,其方法的实现由其类提供。 So, when you instantiate a
Hashtable
object, it uses the implementation already provided by the Hastable
class. 因此,当实例化
Hashtable
对象时,它使用Hastable
类已经提供的实现。
The need for providing an implementation comes when you're creating a class not when instantiating an object from it. 当您创建类而不是实例化类时,需要提供实现。 At instantiation the class must not be abstract ie the implementation should already be present;
在实例化时,该类不能是抽象的,即实现应该已经存在。 either provided by you or like in the case of
Hashtable
by the JDK. 要么由您提供,要么就JDK的
Hashtable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.