aspose file tools*
The moose likes Beginning Java and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
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

Joined: Nov 02, 2004
Posts: 25
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!)


[Jess in Action][AskingGoodQuestions]
Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Nov 02, 2004
Posts: 25
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance