Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java problem

 
Phil Dixon
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working my way through Head First Java.
I'm not sure if I'm missing something or just being stupid. I was hoping that my first post would be a little more taxing than this!

I have two classes. MyAnimalList compiles ok



and AnimalClassTestDrive which will not compile.



I thought maybe it was because I didn't have a Cat or Dog class but then I think the hole point of this exercise is to create a class that can take any kind of subclass.

The error I get is

add(Animal) in MyAnimalList cannot be applies to (Dog
list.add(c);

and the same for cat.

Any guidance will be much appreciated.

Great forum by the way.

Phil.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil,

For those of us who do not have the book to hand: could you please post the Animal, Dog, and Cat classes?

Thanks
-Barry
 
Phil Dixon
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry,

That's all there is. Two classes, MyAnimalList and AnimalTestDrive.

Phil.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have the book with me right now, but I think if you create a couple of classes like:

class Cat extends Animal {
}

and

class Dog extends Animal {
}

your problem will be resolved.

I think the whole point of this exercise is to create a class that can take any kind of subclass.

The problem is that currently you don't have any kind of subclass of Animal for your created class to use.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,

Well you've made it to chapter 8, congrats! We hope you started from the beginning, or at least near the beginning! This chapter relies on chapter 7 as a foundation chapter, so if you just jumped in at chapter 8 you might miss some important stuff.
In any case, based on what you've told us, you must have at least one other class in your directory. Your first assignment is to figure out how I knew that based on your posts... once you've found that class see if you can determine what else you might need. Let us know what you discover.

hth,

Bert
 
Phil Dixon
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marilyn/Bert,

I have read the book from the start ("honest guv").

I tried creating a Dog and Cat class but did not use 'extends Animal'. All working now.

I had/have confused myself how the Animal object is created. I'll read through Ch 7 again and come back with my answer.

Thanks for your help.

[ September 18, 2005: Message edited by: Phil Dixon ]
[ September 18, 2005: Message edited by: Phil Dixon ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic