Complete the TrickCard inheritance free response before reviewing the solution.

Review the TrickCard solution with AP CS Tutor Brandon Horn.

TrickCard solution

public class TrickCard extends Card
{
    private boolean faked;
    private int fakeValue;
    
    public TrickCard(String suit, int realValue, int fakeValue)
    {
        super(suit, realValue);
        this.fakeValue = fakeValue;
        faked = true;
    }
    
    public int getValue()
    {
        if(faked)
            return fakeValue;
        else
            return super.getValue();
    }
    
    public void removeFakeValue()
    {
        faked = false;
    }
}

Explanation

The approach to this problem is the same as for the WeightedDie inheritance free response. The explanation for the method headers and the constructor is nearly identical. getValue is different. removeFakeValue is new.

TrickCard does not need to override toString from Card. The Card method toString calls getValue. The object/instance type determines what method is actually run. If the object type is TrickCard, the call to getValue will call the method from TrickCard. This is true even though the call appears in the Card class.

If the Card class did not have a getValue method, the Card class toString method could not call getValue. See Inheritance and polymorphism for additional discussion.

Comments

Comment on TrickCard