Tuesday, March 13, 2012

Challenge 052’s Solution

Of the five commentators, all five are correct. The first post, written by Dave Dee, claims that the interface class SmartPlayer automatically implements Player’s interface. SmartPlayer inherits all of HumanPlayer’s methods. These methods are implemented from Player, so the statement is true. The second post, written by Dozey, claims that HumanPlayer must implement all of Player’s methods to work. This is true: interfaces, when implemented, should have every method specifically defined within the class that inherits it. The third post, written by Beaky, claims that referencing an object of type Player is not possible. This is true, as interface classes do not have constructors; thus, they cannot be referenced as object types. The fourth post, written by Mick, claims that SmartPlayer can override the defined methods of HumanPlayer. This is true as well, as a subclass is able to override the methods of its superclass. Finally, the fifth post, written by Tich, claims that a client program method can require a parameter of type Player. While this can be required, the usability of the method is questionable, as Player constructors cannot successfully be formed. Thus, on account to technicality, Tich is correct.

−Phoenix

3 comments:

  1. Hi Phoenix, there is one part I'd like to focus on:

    "The third post, written by Beaky, claims that referencing an object of type Player is not possible. This is true, as interface classes do not have constructors; thus, they cannot be referenced as object types."

    There is a difference between "referencing" and "instantiating".

    Research and revise. In your revision, provide an example of a Player object being referenced, but not instantiated.

    ReplyDelete
  2. The third post, written by Beaky, claims that referencing an object of type “Player” is not possible. This is false—a “Player” object can be referenced through either of the following two statements:

         public Player p;

         public void method(Player p){}

    These two statements both reference a “Player” object without instantiating it. However, running “method” will ask for one to define the “Player” object “p”, which isn’t possible, as interface classes lack constructors.

    −Phoenix

    ReplyDelete
  3. You get it. Another example:

    Player p = new HumanPlayer();

    Perfectly legal, as a HumanPlayer "is a" Player.

    ReplyDelete