Java – the process of determining which method in a class hierarchy should execute known as

inheritancejavaobjectobject-orientedpolymorphism

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?

Best Answer

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.

Related Topic