Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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. –  Robert Harvey Apr 14 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? –  user3533767 Apr 14 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 –  user3533767 Apr 14 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 at 2:31

2 Answers 2

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.

share|improve this answer
6  
This is a terrible question. The determining part is "dynamic dispatch," which comes into play when you have polymorphism. –  mgw854 Apr 14 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 at 2:52

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.

share|improve this answer
    
I think this is as precise as one can be. At most, I would flirt with the idea of generalizing the enumeration under "binding". –  Thiago Silva Apr 15 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. –  Jörg W Mittag Apr 15 at 0:30
    
In Java, the term is virtual method invocation. It's an aspect of Polymorphism in Java. –  Derek W Apr 15 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: […]" –  Jörg W Mittag Apr 15 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. –  Jörg W Mittag Apr 15 at 10:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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