Suppose that Fruit, Apple, Orange, GoldenDelicious, and McIntosh are defined in the following inheritance hierarchy:
Assume that the following code is given: Fruit fruit = new GoldenDelicious(); Orange orange = new Orange(); Apple goldenDelicious = new Apple(); Answer the following questions with a brief justification: a) Is fruit instanceof GoldenDelicious? b) Is orange instanceof Apple? c) Is goldenDelicious instanceof GoldenDelicious? d) Suppose the method makeAppleJuice is defined in the Apple class. Can goldenDelicious invoke this method? Can fruit invoke this method? e) Is the statement GoldenDelicious p = new Fruit() legal? |
ANS:
a. Yes, fruit is an instanceof Fruit as fruit is initiated with the constructor of GoldenDelicious class which is a subclass of Apple class which is a subclass of Fruit.
b. No, fruit is not an instanceof Orange as fruit is initiated with the constructor of GoldenDelicious class which is a subclass of Apple class which is a subclass of Fruit class so, it is not connected with Orange class.
c. Yes, fruit is an instance of Apple as fruit is initiated with the constructor of GoldenDelicious class which is a subclass of Apple class
d. Yes, fruit is an instance of GoldenDelicious as fruit is initiated with the constructor of GoldenDelicious class
Comments
Post a Comment