Friday, December 26, 2014

[Java] 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();


Answer the following questions:
  1. Is fruit instanceof Fruit?
  2. Is fruit instanceof Orange?
  3. Is fruit instanceof Apple?
  4. Is fruit instanceof GoldenDelicious?
  5. Is fruit instanceof McIntosh?
  6. Is orange instanceof Orange?
  7. Is orange instanceof Fruit?
  8. Is orange instanceof Apple?
  9. Suppose the method makeAppleCider is defined in the Apple class. Can fruit invoke this method? Can orange invoke this method?
  10. Suppose the method makeOrangeJuice is defined in the Orange class. Can orange invoke this method? Can fruit invoke this method?
  11. Is the statement Orange p = new Apple() legal?
  12. Is the statement McIntosh p = new Apple() legal?
  13. Is the statement Apple p = new McIntosh() legal?


Answer:

Here is the testing code.
public class Scjp {
    public static void main(String[] args) {
        Fruit fruit = new GoldenDelicious();
        Orange orange = new Orange();
        if(fruit instanceof Fruit){ //Change this line for test
            System.out.println("true");
        }else{
            System.out.println("false");
        }
    }
}
class Fruit{}
class Apple extends Fruit{}
class Orange extends Fruit{}
class GoldenDelicious extends Apple{}
class McIntosh extends Apple{}

  1. Is fruit instanceof Fruit true? true
          
  1. Is fruit instanceof Orange true? false
          
  1. Is fruit instanceof Apple true? true
          
  1. Is fruit instanceof GoldDelicious true? true
          
  1. Is fruit instanceof Macintosh true? false
          
  1. Is orange instanceof Orange true? true
          
  1. Is orange instanceof Fruit true? true
          
  1. Is orange instanceof Apple true? False
          

  1. Suppose the method makeAppleCider is defined in the Apple class.
    1.      Can fruit invoke this method? Yes

public class Scjp {
   public static void main(String[] args) {
       Fruit fruit = new GoldenDelicious();
       Orange orange = new Orange();
       ((GoldenDelicious)fruit).makeAppleCider();//precedes casting
       if(fruit instanceof Orange){ //Change this line
           System.out.println("true");
       }else{
           System.out.println("false");
       }
   }
       
}
class Fruit{}
class Apple extends Fruit{
   public void makeAppleCider(){}
}
class Orange extends Fruit{}
class GoldenDelicious extends Apple{}
class McIntosh extends Apple{}

    1.      Can orange invoke this method? No : object orange is not an instance of Apple class, it can’t invoke method makeAppleCider() in class Apple.

  1. Suppose the method makeOrangeJuice is defined in the Orange class.
    1.      Can orange invoke this method? Yes.

public class Scjp {
   public static void main(String[] args) {
       Fruit fruit = new GoldenDelicious();
       Orange orange = new Orange();
       orange.makeOrangeJuice();
       if(fruit instanceof Orange){ //Change this line
           System.out.println("true");
       }else{
           System.out.println("false");
       }
   }
       
}
class Fruit{}
class Apple extends Fruit{}
class Orange extends Fruit{
   public void makeOrangeJuice(){}
}
class GoldenDelicious extends Apple{}
class McIntosh extends Apple{}

    1.      Can fruit invoke this method? No : object fruit is not an instance of Orange class, it can’t invoke method makeOrangeJuice() in class Orange.

  1. Is the statement Orange p = new Apple() legal? No
   
  1. Is the statement Macintosh p = new Apple() legal? No
Is the statement Apple p = new Macintosh() legal? Yes


Remarks
Text provided above does not means is the model answer, and it's was posted here for an reference to somebody who interested the related knowledge only. if there is something incorrect, please leave a comment so that i can correct it or for others viewer to have an better reference, if you want know more about this topic and where is the question, concept or answer from, you can reference from information provided under the reference sub-title.

Reference:
1) Introduction to Java Programming, Y. Daniel Liang , 10th , 11.26

1 comment :