Friday, May 21, 2010

AP computer science ab question?

public interface Controller





public class Widget implements Controller





public class Thingy extends Widget





Consider the following declarations.





I. Widget myThing = new Thingy();





II. Controller myWidge = new Thingy();





III. Controller myControl = new Controller();





Which of these declarations will compile correctly?





A. I only


B. II only


C. III only


D. I and II


E. II and III











Assume that the following partial declarations have been made, with default constructors for the classes.





public interface Controller





public class Widget implements Controller





public class Thingy extends Widget





Consider the following declarations.





I. Widget myThing = new Thingy();





II. Thingy myWidge = new Widget();





III. Controller myControl = new Thingy();





Which of these declarations will compile correctly?





A. I only


B. I and II only


C. I and III only


D. II and III only


E. I, II, and III











I know the answers are both D, but can anyone plz explain to me why?

AP computer science ab question?
I.Widget myThing = new Thingy();





because Thingy is a subclass of Widget, it can be used anywhere its superclass is expected.





II.Controller myWidge = new Thingy();





same as above, except Controller is an interface, and Thingy is a subclass of a class that implements that interface





II.Thingy myWidge = new Widget();





I don't think this is right. Thingy will have members that Widget does not, so this won't compile because then you'd be able to reference a member that Widget lacks.





III.Controller myControl = new Thingy();





Same as first two.





I. should compile correctly too.

bouquet

No comments:

Post a Comment