137 views
1 vote
What method is run in the following: Card crd = new BirthDay("Lucinda", 42);

crd.greeting();
The type of the object or the type of the reference variable?
a. The type of the object.
b. The type of the reference variable.

User Amitlicht
by
7.5k points

1 Answer

3 votes

Final answer:

The method that is executed is determined by the type of the object, which allows dynamic method dispatch or polymorphism. So, the greeting method of the BirthDay class is run, regardless of the reference variable type being Card.

Step-by-step explanation:

The question is inquiring about what method is run when the code Card crd = new BirthDay("Lucinda", 42); crd.greeting(); is executed. Specifically, it is asking whether the method that is executed depends on the type of the object or the type of the reference variable. In Java and other object-oriented programming languages, the actual method that will be run is determined by the type of the object at runtime, which is known as dynamic method dispatch or polymorphism. So in this case, even though crd is a reference variable of type Card, the greeting method of the BirthDay class will be run because crd is actually referring to an instance of the BirthDay class.

User Aquila Sands
by
7.5k points