What does mean by saying polymorphism allows programming in general rather then programming in specific?

Recommended Answers

All 5 Replies

If you break up the root parts of the word you get "poly" meaning "many" and "morphism" meaning "form" (loosely). Take method names. Let's say you want a method to find the absolute value of a number. In ore-OOP programming you would need (this is not necessarily a complete list


to take the absolute value of an integer, float or complex number. In other words, you would be programming for the specific case. In OOP you would define three methods, all called abs but with differently typed parameters. Your calling code would now use the general name abs rather than the specific name for each type.

Is this the same meaning of the question here : enables you to write programs that process objects that share the same superclass (either directly or indirectly) as if they’re all objects of the superclass; ?

That sounds like a textbook definition that to me (a dinosaur) doesn't really clarify anything. Mostly because I've never been a fan of buzzwords. I prefer explaining (and learn better) by analogy or example.

Let's say you have a superclass of type mammal. This class can contain methods common to all mammals (the general case). You program these methods once. You subclass this class for the specific case (kangaroo, wolverine, etc) and add (or override) methods for the specific case.

To clarify, each mammal (or even animal in the more general case) could have a Move method. The calling code would not have to be concerned with how Move differs from animal to animal. It would only care about the general case, leaving the specifics of how Move is handled to the individual classes.

Just for clarity:
There are two types of polymorphism, and one thing that isn't called polymorphism, in this discussion

Overloading: You have multiple methods with the same name but different parameter types. The correct method is chosen at compile time based on the actual types of the parameters that you pass to it. This is a kind of polymorphism.

Overriding: A subclass implements a method with the same name and argument types as one in the superclass. The correct method is chosen at runtime based on the actual class of the object. This is the other kind of polymorphism.

Inheritance: You have a method that takes a parameter of a certain class. You call it passing an instance of a subclass of that class. It works because of inheritance. In this case there is just one method defintion, so it's not polymorphism.

If you want the definitive answer as to how Java selects which method to use, it's documented in the Java Language Specification Section 15.12
Be warned this includes what happens if you have Generics and/or variable numbers of parameters. It's not for the faint-hearted!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.