简体   繁体   中英

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:

When I create a program that instantiates a Hashtable object, why am I not required to define every method in the Map interface? 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. If you created a class which implements Map , the compiler would ask you to implement all the necessary methods.

Does the Hashtable implement every method in the Map interface?

Yes. If you take a look at the JavaDoc the Hashtable class has been defined as

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 . Hence, it must and it does implement all the methods defined in Map interface.

When I create a program that instantiates a Hashtable object, why am I not required to define every method in the Map interface?

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.

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.

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