Suppose that Fruit Apple Orange GoldenDelicious and Macintos

Suppose that Fruit, Apple, Orange, GoldenDelicious, and Macintosh are declared, as shown in the following figure: Assume that the following declaration is given: Fruit fruit = new GoldenDelicious(); Orange orange - new Orange(); Answer the following questions: Y (yes) or N (No) Is fruit instanceof Apple? Is fruit instance of Orange? Is fruit instance of Macintosh? Is orange instanceof Fruit? Is orange instance of Apple? Suppose the method makeAppleCider is defined in the Apple Can fruit invoke this method? Suppose the method makeOrangeJuice is defined in the Orange Can orange invoke this method? Can fruit invoke this method? Is the statement Orange p - new Apple()legal? Is the statement Macintosh p - new Apple()legal? Is the statement Apple p - new Macintosh()legal?

Solution

1. No, fruit is not an instance of Apple because it is an instance of its super class Fruit so it will create a type conversion error and moreover this object will not be able to access the method of Apple class.

2. No, fruit is not instance of Orange because fruit is an instance of Fruit class and moreover, Apple and Orange are different classes. So, an instance of Fruit class cannot be an instance of Orange class.

3. No, fruit cannot be an instance of Macintosh because Fruit and Macintosh are different classes. So, an instance of Fruit class cannot be an instance of Macintosh class.

4. Yes, orange is an instance of Fruit because orange is an instance of Fruit\'s child class Orange. So, an instance of Orange class automatically becomes an instance of Fruit class.

5. No, orange is not an instance of Apple because it is an instance of Orange class and moreover Apple and Orange are different classes. So, an instance of Orangle class cannot be an instance of Apple class.

6.No, it cannot invoke this method because it is an instance of Fruit class and an instance created from super class cannot access the methods of its child classes. So, an instance of Fruit class will not be able to access makeAppleCider function of Apple class.

7. Yes, an instance of Orange class will invoke the method makeOrangeJuice method of Orange class. No, fruit cannot invoke this method because it is an instance of Fruit class and both Fruit and Orange are different classes and moreover an instance created from super class cannot access the methods of its child classes, so it will not be able to access the makeOrangeJuice method of Orange class.

8. No, it is not legal because both Apple and Orange are different classes.

9. No, it is not legal because it is not possible to create an instance of child class from its super class which will generate an error. So, the declaration is not legal.

10. Yes, the declaration is legal because we can create an instance of super class from its child class which will not create any error. So, the declaration is legal.

 Suppose that Fruit, Apple, Orange, GoldenDelicious, and Macintosh are declared, as shown in the following figure: Assume that the following declaration is give

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site