Java – is generic programming an example of polymorphism?

I'm doing a homework (a project) and one of the criteria is that I must take advantage of polymorphism in a way that significantly improves the overall quality or function of the code

I made a hash table that looks like this:

public class HashTable<E extends Hashable>{
    ...
}

Hashable is an interface with hash () function I made

I know that using generics can improve the quality of my code, because now hashtables can use any type I want (for example, not just int or strings) But I'm not sure if it shows polymorphism

I think so, because e can be any type that implements hashable In other words, a hashtable is a class that can (actually) use any type

But I'm not sure - is this polymorphic? Maybe I can get some clarification about what polymorphism is?

Thank you in advance!

Editor: after receiving the following answer (see: first answer received), I read the Wikipedia article and found this:

"In the object-oriented programming community, programming using parameter polymorphism is usually called generic programming." (key supplement)

Solution

In short, polymorphism adopts many different classes. They share a common base class (or interface) and treat them all as members of the common base, without knowing or caring which specific inheritance class they are or how they accurately implement the functions of the common base class In object-oriented programming, polymorphism provides you with a relatively high-level view of that part of the problem domain - you say, "this code doesn't care about the specific details of these objects - it's just that they implement some common functions x, y, Z," or "they're all basic classes “

Your example uses polymorphism because you define the hashable base class - and provide functions that focus only on this aspect of the object it receives Hashable objects can take many forms, but your code treats them all as a single basic type

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>