|
Do you think it would be OK to do the following?
A good answer might be:Yes, both are correct, since Holiday is-a Card and Birthday is-a Card. |
Using Parent Class Reference VariablesA reference variable of class "C" can be used with any object that is related by inheritance to class "C". For example, a Card reference variable card2 can hold a reference to a Holiday object, a Valentine object, or a Birthday object. Usually, references to a parent class are used to hold children objects. Important Point: When a method is invoked, it is the class of the object (not of the variable) that determines which method is run.For example: This will run the greeting() method for a Valentine, then it will run the greeting() method for a Holiday, then it will run the greeting() method for a Birthday. The type of the object in each case determines which version of the method is run.Card card = new Valentine( "Joe", 14 ) ; card.greeting(); Card card2 = new Holiday( "Bob" ) ; card2.greeting(); Card card3 = new Birthday( "Emily", 12 ) ; card3.greeting(); |
QUESTION 12:Is it necessary to use three different variables for this program fragment? Click Here after you have answered the question |