Thursday, March 22, 2012

challenge 052

The first statement is true due to high-order inheritance. SmartPlayer implements the Player interface because HumanPlayer implements the interface which has SmartPlayer as a subclass, therefore being implemented with HumanPlayer due to inheritance.

The second statement is also true because HumanPlayer implements the interface Player. Player contains the methods int getMove() and void updateDisplay() and you stated that HumanPlayer implements the Player interface, so it also inherits the two methods within the class.

The third statement is false. You are able to declare a reference of type Player because HumanPlayer and SmartPlayer share the Player interface. To reference variables, the types need to share a superclass or interface, which they do (Player interface). Player can therefore be referenced because Player also refers to HumanPlayer and SmartPlayer and is in the Player interface.
(I used the following website to help me understand more about referencing:
http://stackoverflow.com/questions/4207732/how-to-declare-object-reference-variables-and-assign-them-values-according-to-us)

The fourth statement is true because in inheritance, a method that is declared can be overridden by any class that extends the primary class. SmartPlayer inherits the Player interface and its components and is able to alter/override any method.

The fifth statement is true because any interface that has the capability of being referenced can hold a parameter. Since HumanPlayer and SmartPlayer inherit the Player interface, it can take parameters for the HumanPlayer and SmartPlayer method.

7 comments:

  1. Okay, so would the compiler go nuts if I did something like this?

    Player p = new SmartPlayer();

    ReplyDelete
  2. That would be okay to do because Player p = new SmartPlayer(); references a Player object which is inherited in the superclass

    ReplyDelete
  3. In the relationship between the Player interface and the SmartPlayer class, which is the superclass and which is the subclass? Please explain your response. (If you don't see this until after Friday, midnight, you can still respond for credit.)

    ReplyDelete
  4. Player interface is the super class and SmartPlayer inherits Player interface and is the subclass. So in the above response, Player p = new SmartPlayer(); refers to a Player object, which is contained in the superclass, Player interface

    ReplyDelete
  5. Ultimately you're good here.

    I want to clean this up a bit:

    "Player p = new SmartPlayer(); refers to a Player object, which is contained in the superclass, Player interface"

    It's more like this:

    "Player p = new SmartPlayer();"

    Creates a reference named "p", of type "Player". That is to say, that a box in computer memory holds a reference labeled "p", and it refers to a Player data type.

    The actual instance of an object in computer memory that "p" refers to is of type SmartPlayer.

    Does this make sense? If not, ask. Otherwise, 300 XPs awarded for completion.

    ReplyDelete
  6. okay thank you. everything makes sense to me except for the fact that you said "p" is of type "Player" and then you go on to say "p" is type SmartPlayer. Can "p" because part of/and or both? Is this because SmartPlayer inherits Player? And therefore is SmartPlayer of type "Player"?

    (Haha I hope you understand what I'm asking because it's a little confusing even when I read what I wrote.)

    I guess I just dont totally understand "of type..._____" do you mind explaining that a little further?

    ReplyDelete
  7. Is this because SmartPlayer inherits Player? Yes.

    There is a box setup in computer memory, that holds a reference to an object.

    The box itself if of type Player, and e call that box (variable) "p", as in:

    Player p =

    That box basically holds a reference, a pointer to an actual object created in computer memory, and that object is an actual instance of a SmartPlayer, as in:

    new SmartPlayer();

    ReplyDelete