6

I thought I understood inheritance and polymorphism, but I was given this question, and I can't, for the life of me, figure out what the proper answer is or what they're trying to get at:

The process of determining which method in a class hierarchy should execute is known as:

  • a) inheritance
  • b) polymorphism
  • c) is-a
  • d) has-a
  • e) parent class

Looking at each of the terms, none of them seem like the proper answer.

Inheritance is just when a class automatically gets the public variables and methods in its parent class. So this clearly isn't the right answer.

Polymorphism allows us to write one method to handle object A, and as a result will work with everything that extends object A (or continues to extend it, IE Object B extends Object A. Object C extends Object B, etc) So this clearly isn't the right answer!

Is-a: This doesn't even make any sense. Is-a is just used to declare that a class is an instance of its parent class (dog is-an animal), so it inherits its public variables and methods. I don't see how this is "determining which method in a class hierarchy should execute"

Has-a: I'm not too familiar with this, but it's essentially composition, where Object-A has-a Object B, but object-B isn't an instance of Object A. This doesn't seem like the right answer either

Parent Class: This is just the base class, if we trace the tree of inheritance up, the top of the tree is the parent class.

Can someone please explain which term can also be defined as "The process of determining which method in a class hierarchy should execute is known as"?

Am I not understanding one or more of the terms? Is this simply a poorly worded question?

8
  • 5
    Hint: There's more than one kind of polymorphism. FWIW the question is a terrible one; to answer it correctly, you'll probably have to find its definition in your textbook. Apr 14, 2014 at 22:11
  • It's unfortunately an online AP computer science class. I've checked the online "glossary" they provide, and this definition matches none of them. Do you think polymorphism is the most probable answer to go with? Apr 14, 2014 at 22:15
  • "It is also known as function overloading or operator overloading." Thanks so much for the help! If this question-writers define polymorphism this way, then it should be best answer Apr 14, 2014 at 22:19
  • Just a note on your definition of inheritance. A subclass inherits all of the public and protected members of its parent - not just public members as you have stated a couple of times here. My overall vote is in the polymorphism group as well - definitely not the greatest question.
    – Derek W
    Apr 15, 2014 at 2:31

2 Answers 2

11

The question asks "which process determines which method should execute?"

This is a bad question.

But, we can immediately eliminate three of the choices: Is-A, Has-A, and Parent Class, since those are object-oriented, but not certainly not processes. Even if Is-A and Has-A were processes, they would be processes regarding class and composition, as you said, not method execution.

Inheritance is a process (also a concept), a process of inheriting the traits of the parent class. By that definition alone, we can eliminate it.

That leaves us with only Polymorphism, which is part of why this is a bad question. Polymorphism sounds like a concept, more than a process. However, if considered to be a process, it would be a process of "polymorphing" or "taking a different shape". An answer on Stack Overflow says:

So polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).

By that definition, we're talking about a process where an object can act as one or more underlying real types. You have an interface, and the underlying data types determine which method should actually be called.

We're looking for a process that matches the statement "determining which method in a class hierarchy should execute", and I think that matches pretty well.

It's also a bad question because that statement could mean many other things, and I don't think many people would really described the "determining" part to be the polymorphism -- instead, I'd consider polymorphism to be the definition part.

2
  • 6
    This is a terrible question. The determining part is "dynamic dispatch," which comes into play when you have polymorphism.
    – mgw854
    Apr 14, 2014 at 23:12
  • 1
    What the authors of the question describe in known as virtual method invocation in Java which is an aspect of Polymorphism in the Java language. Polymorphism definitely isn't a very specific answer, but as you have deduced it's the best answer we got.
    – Derek W
    Apr 15, 2014 at 2:52
6

None of the above. There is no universal term, it depends on the language and the community, but the correct answer is one of

  • message dispatch
  • (virtual) method dispatch
  • (virtual) method resolution
  • (virtual) method lookup
  • vtable lookup

or a similar term.

6
  • I think this is as precise as one can be. At most, I would flirt with the idea of generalizing the enumeration under "binding". Apr 15, 2014 at 0:23
  • @ThiagoSilva: That's what I thought … until yesterday, when I finally read an article that explained the difference: binding resolves a name to a thing, in this case an operation. Dispatch resolves a polymorphic operation to a concrete implementation. IOW: binding is independent of polymorphism, dispatch is the process of picking a concrete implementation of a polymorphic operation. Apr 15, 2014 at 0:30
  • In Java, the term is virtual method invocation. It's an aspect of Polymorphism in Java.
    – Derek W
    Apr 15, 2014 at 2:42
  • 1
    @DerekW: the Java Language Specification uses the term "method lookup": "Otherwise, the invocation mode is interface, virtual, or super, and overriding may occur. A dynamic method lookup is used. The dynamic lookup process starts from a class S, determined as follows: […]" Apr 15, 2014 at 10:16
  • @DerekW: "virtual method invocation" means calling a method you don't (yet) know the implementation of. But that's not what the question asks. The question asks about what the process is that resolves a virtual method invocation to a concrete implementation. That's a fundamentally different question, in fact, it goes to the very heart of understanding what ad-hoc polymorphism means. Apr 15, 2014 at 10:20

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.