Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Amy Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm constructing a StandardDeck class that inherits from a Card class. I've compiled my code and am receiving the error "Card(int,int) in Card cannot be applied to()." Does anyone know what this means? Here is my code for the StandardDeck class:



Thanks!
 
Amy Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone could help me with this, I would REALLY appreciate it - let me know if I need to give more information. THANKS!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that Card has only one constructor, and it takes two ints as arguments, yet StandardDeck's constructor doesn't arrange to call it. You'd do this using a call like:

super(intx, inty);

Alternatively, you could add a no-argument "default" constructor to Card; this is the one that StandardDeck calls implicitly.

Now, I have to point out that a Deck isn't a kind of Card, and it seems to me that you've already got many Deck-like features in your Card class, so you need to give some serious thought as to whether having Deck inherit from Card makes any sense (hint: it doesn't!)
 
Amy Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback - I thought the same thing when I extended Card, but I wasn't sure if I was correct. My teacher gave us an assignment and he provided a Card class for us to use, yet he wants us to create a StandardDeck class (and a Hand class). I automatically thought "extend Card," but I see that I'm wrong.

How can I call methods from the Card class without extending it to StandardDeck?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Deck would presumably own a collection of Card objects, and similarly a hand might also be a (smaller) collection of Cards. A function that scores a hand might look like the following: it adds up the values of each card by calling getValue() on each Card object in the collection. The variable "card" refers to each card in the hand in turn, and this code uses that variable to call Card's methods.

[URL]
public int score(List hand) {
int score = 0;
for (Iterator it = hand.iterator(); it.hasNext() {
Card card = (Card) it.next();
score += card.getValue();
}
return score;
}
[/code]
 
Amy Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic