0

A number of tutorials on polymorphism state that "Any object that can pass more than one IS-A test is considered to be polymorphic." I wonder what they mean by that, and if that's even a true statement.

From my understanding, (subtype) polymorphism is when you have the interface of the base class and the implementation of the derived class. Which would look like this:

Shape myShape = new Circle();

But the implication of every object that passes more than one IS-A test being polymorphic is that every object of every class in Java or C# is polymorphic all the time. ([With the exception of objects of type Object.] Since every class inherits from Object.) In other words this would be an example of polymorphism:

Shape myShape = new Shape();

This doesn't make sense to me since we know for a fact that inheritance and polymorphism are two separate concepts. But this implies that they are effectively the same thing, because as soon as a class inherits from another class, it becomes polymorphic.

I've tried to figure out some possible explanations as to why they said that. These are my guesses:

  • Maybe being "polymorphic" and using polymorphism are two different things. It could be that saying that a class is polymorphic doesn't necessarily mean that the class is using polymorphism at that exact moment but rather that it has the potential of using polymorphism.
  • Given the fact that polymorphism is possible due to the CLR using a virtual function table, maybe the CLR uses the vtable in a similar way during inheritance as it does during polymorphism, and that is why they said that a class that passes multiple IS-A tests is polymorphic. (idk, this is just a guess.)
  • According to Bob Martin, phrases like "IS-A" and "HAS-A" are remnants of artificial intelligence programming and don't accurately reflect the relationships between classes in OOP. However, if a class is more than one thing, then it would take "many forms" in a vague interpretation of the word "polymorphism." We know that "polymorphism" is such a vague word that it can even include ad-hoc polymorphism (method overloading), even though it's not really polymorphism.

I don't know. These are just guesses as to why they said that a class that can pass more than one IS-A test is polymorphic. And to clarify, the article is speaking about subtype polymorphism in Java. Although, I think it works the same way as in C#.

3
  • 2
    It is easy to prove this statement is incorrect. Suppose a class offers two instance methods that are independent of one another, A and B. An instance of such a class is an object supplying method A, and it is an object supplying method B. As soon as you aggregate any functionality at all, the statement is false. Oct 3, 2017 at 0:22
  • @FrankHileman I'm sorry I'm not following. Which statement is false? Also, what do you mean by "aggregate any functionality?" Oct 4, 2017 at 19:25
  • The quoted statement at the top of your question. Assume the class with the instance methods is not polymorphic; i.e. these members are not virtual. "Aggregate any functionality" means put together more than one independent operation or bit of data in the class. Oct 4, 2017 at 23:05

3 Answers 3

7

You are confused because you think of inheritance and polymorphism as separate concepts. But in fact, inheritance is one possible way of implementing polymorphism, which makes the latter the more general concept, and inheritance a special form of the former.

So yes, as you wrote (and as also stated in the article you linked to), every object of every class in Java or C# is polymorphic, since it they all derive from Object. And yes, "as soon as a class inherits from another class, it becomes polymorphic" - that's correct. However, the "opposite" is not mandatory: when objects are polymorphic, that does not necessarily mean there is inheritance involved.

This is probably easier to understand when you have seen examples of polymorphism without inheritance, like Microsoft's component technology COM, which does not even implement inheritance. For example, using late binding for COM components, it is possible to use objects in a polymorphic way without a common base class or an explicitly defined common interface. Another example is "static polymorphism" in C++ template meta programming, or polymorphic functions in Python, like shown here.

8
  • Thanks for your reply. The article I linked was referring to subtype polymorphism, which by definition is achieved only through inheritance. I should have been more specific with the scope of my question, namely subtype polymorphism in C# (or Java). Oct 4, 2017 at 21:06
  • @wolfrevo_kcats: sure it is. That's exactly the point of my answer. The article starts with polymorphism in its general meaning, and then explains how it is achieved in Java using inheritance. It seems you believe my answer does not apply to your question. It does, if you have problems understand why, let me know exactly what's not understandable.
    – Doc Brown
    Oct 4, 2017 at 21:14
  • "as soon as a class inherits...." Suppose internal abstract class A has only one derived class, B. Is an instance of A polymorphic (ignoring the mandatory Object root)? I would say not, as we can tell from static analysis that an instance of A must be an instance of B. If A was public, anyone could extend it, and the argument breaks down. Oct 4, 2017 at 23:09
  • @FrankHileman: nitty, nitty ;-) My answer tries to explain what the original article meant, and where the OPs confusion comes from, no less, no more. What you mention is IMHO a debatable edge case.
    – Doc Brown
    Oct 5, 2017 at 11:39
  • Well, that is not the only case. Suppose the base class A has no virtual members. A call on a derived instance, when cast as A, cannot be polymorphic. For me, the definition has to do with whether a call site can be resolved at compile time or run-time, specifically with instance calls. Oct 5, 2017 at 18:48
3

I think of polymorphism as a property of a design. If you have an interface (whether a "true" interface, or an abstract base class, or generic, or other) and that interface is implemented by more than one actual type that can have instances, that is a design involving polymorphism.

I don't really think of polymorphism as being a quality of a single instance.

From the wikipdia article on polymorphism, which is quoting Stroustrup:

... polymorphism is the provision of a single interface to entities of different types

Thus, some of us don't talk about a single object instance as exhibiting polymorphism, or as being polymorphic.

The way I see it, it is less about a single object that conforms to more than one interface (i.e. satisfies more than one is-a), and more about one interface having multiple implementations. It is true that it is hard to show an example of one without bringing up the other, so I see it as a matter of focus.

2
  • Stroustrup would be referring to any class with virtual methods, since C++ has no interfaces. He is using the word in the general sense. Oct 3, 2017 at 0:26
  • @FrankHileman, yes, good point to call out. I am also attempting to use the word broadly rather than as some language-specific keyword.
    – Erik Eidt
    Oct 3, 2017 at 2:35
0

Using an object polymorphically just means using it in a way that the user does not need to know what it really is, just how to access some properties or behavior.
Programming to the interfece/contract instead of by trial and error is especially crucial for that.

That can be achieved in many ways, like:

  • Traits, like in Rust: A pointer to the object, and a list of functions accepting that pointer to do things.
    A restricted example of traits are delegates, like in C# and Java, or std::function in C++.
  • Duck-typing, like statically with C++ templates, or dynamically with most scripting languages.
  • Class-based dynamic polymorphism, referring to an object of a class by any base-class/interface, or even the actual type if that can be further derived.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.