|
I want to start off by saying I do have information about each of these questions in a text book I was given but like most text books the examples they give are a lot easier to see how they are solved then actually solving the problems they give. But I gave each one a shot. In question 1 I was supposed to figure out whether or not each action was legal.
1. Suppose the class Sandwich implements the Edible interface, and you are given the variable definitions Sandwich sub = new Sandwich(); Rectangle cerealBox = new Rectangle(5, 10, 20, 30); Edible e = null; Which of the following assignment statements are legal? a. e = sub; b. sub = e; c. sub = (Sandwich) e; d. sub = (Sandwich) cerealBox; e. e = cerealBox; f. e = (Edible) cerealBox; g. e = (Rectangle) cerealBox; h. e = (Rectangle) null
My answers are below with reasoning.
a. e = sub; is not legal because not everything that is edible is going to be a sub b. sub = e; is legal because every sub is edible c. sub = (Sandwich) e; is legal because each sub is going to be a sandwich which is edible d. sub = (Sandwich) cerealBox; is not legal because a sub does not make a sandwich that is in class cerealBox (From here on is where I started to get confused) e. e = cerealBox; is not legal because cerealBox is not edible f. e = (Edible) cerealBox; is legal because in this case cerealBox is edible g. e = (Rectangle) cerealBox; is not legal becuase a rectangular cerealBox is not edible h. e = (Rectangle) null; is legal because edible is whatever class null is going to be legal
Note: For my assignment I do not need the reasoning why one is legal or not but wanted to show that I at least attempted to use reasoning on why one was legal or not. Once again if you think my reasoning makes no sense it most likely won't the examples I was given didn't really give me any help so I'm trying to figure out exactly how this would work.
2. Suppose r contains a reference to a new Rectangle(5, 10, 20, 30). Which of the following assignments is legal? a. Rectangle a = r; legal because this is just stating that their is a rectangle called Rectangle a and it has the same coordinates as the new Rectangle b. Shape b = r; legal because Rectangle is under the Shape hierarchy c. String c = r; legal because the String would have the same values as Rectangle r d. ActionListener d = r; I could not find anything about ActionListener but I'm guessing not legal? e. Measurable e = r; not legal I'm pretty sure that measureable refers to text? f. Serializable f = r; I was looking the the java.sun website and I think from what I saw that this would be legal g. Object g = r; legal because Rectangle is under the hierarchy of class Object
3. Classes such as Rectangle2D.Double, Ellipse2D.Double and Line2D.Double implement the Shape interface. The Shape interface has a method Rectangle getBounds() that returns a rectangle completely enclosing the shape. Consider the method call: Shape s = . . .; Rectangle r = s.getBounds(); Explain why this is an example of polymorphism.
I think this is an example of polymorphism because it allows the values of shape s, rectangle r. , and s.getBounds to be handled using a single interface.
|