views:

200

answers:

4

I'm working on a homework assignment (a project), for which one criterion is that I must make use of polymorphism in a way which noticeably improves the overall quality or functionality of my code.

I made a Hash Table which looks like this:

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

where Hashable is an interface I made that has a hash() function.

I know that using generics this way improves the quality of my code, since now HashTable can work with pretty much any type I want (instead of just ints or Strings for example). But I'm not sure if it demonstrates polymorphism.

I think it does, because E can be any type that implements Hashable. In other words HashTable is a class which can work with (practically) any type.

But I'm not quite sure - is that polymorphism? Perhaps can I get some clarification as to what exactly polymorphism is?

Thanks in advance!


Edit: After receiving the answer below (see: first answer received), I read the Wikipedia article, and came across this:

"In the object-oriented programming community, programming using parametric polymorphism is often called generic programming." (emphasis added)

+4  A: 

Yes, it's called parametric polymorphism. Your reasoning for classing it as polymorphism is spot-on.

Rob
Awesome - Thanks!
CS Student
+1: Hopefully the asker's teachers won't mark him down for thinking of something they didn't but that doesn't change the +1ness of your answer or the +1ness of the asker's assessment.
As long as the student refers to it as "parametric polymorphism" and gives a clear reasoning why and how it is so then he should be okay. If the teacher marks him down then the teacher really shouldn't be teaching.
Spoike
A: 

While Rob's answer might be true in some technical sense, I don't think it's what is being asked for. I think this wikipedia article is more likely to be what you need.

John
Can you elaborate on that?
CS Student
@CS Student: Generic programming has some important differences from more "classic" polymorphism. Templated code basically generates a new class/function for each type you call the code with. Instead of saying "this code refers to objects only at the level of their common base class," generic programming is often saying "you can plug any typename you want to into this code, and it will work" - there's no common base class, no reliance on anything in common to the many forms the code can handle.
Ziv
(continued) In your example, you avoid this, because you limit that "any typename" to a subset of typenames which is, in itself, polymorphic. There are other differences as well (e.g. classic polymorphism can deal with different classes dynamically, while templates have to be instantiated at compiletime - you can't instantiate a template to a typename which is determined during the program's run).
Ziv
Your HashTable<E> is treating subclasses of Objects as Objects, and is therefore technically polymorphism. I also agree that generics are useful and morally good. However, I still don't think it's a good example: I think it would be difficult for students to learn about polymorphism using this example. In my opinion, a *proper* example of polymorphism requires two different subclasses to be manipulated (as instances of the superclass) by one piece of code.It's fine for you to choose a marginal(?) example, be glad that you know better than the teacher, and maybe have to argue for your marks.
John
Right - the use of templates alone, I think, would be more iffy. Since he's treating subclasses of `Hashable` as Hashables, I think he's in the clear - that's where I'd put the emphasis.
Ziv
+3  A: 

In addition to parametric polymorphism, you'll presumably be calling the hashCode method of objects stored in the hash table, which is an example of the polymorphism John refers to. HashTable relies on parametric polymorphism, and E relies on (plain) polymorphism.

outis
+3  A: 

Polymorphism is, in a nutshell, taking many different classes which share a common base class (or interface), and treating them all as members of that common base, without knowing or caring which particular inheriting class they are or how precisely they implement the common functions. In object oriented programming, polymorphism gives you a relatively high-level view of that part of your problem domain - you're saying, "This code doesn't care about the specific details of these objects - just that they implement some common functions X,Y,Z," or "just that they're all of the basic class BaseClass."

Your example uses polymorphism, because you define a Hashable base class - and provide a function that cares only about that aspect of the objects it receives. Hashable objects may have many forms, but your code treats them all as a single basic type.

Ziv
Switched the best answer to this one - this is straight to the point and very helpful; Thanks!
CS Student