Results 1 to 5 of 5
Thread: Head First Java Book question
- 05-02-2011, 07:05 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Head First Java Book question
Hi, yep it's me again. I have another "Head First Java Book" question if anyone has that book that would be great. The page is page 207. It is an example of making classes more generic so that they can be used by more objects. (I think objects). It's like you make MyAnimalList, instead of ListADog, and ListaCat classes. Generic is better.
My question is. They create a new instance of Cat() and Dog() but have no class declaration on the page, in the code, of those. So I have no idea how the whole complete code would look. I think it is only part done on the page. Any help Greatly appreciated. thank you. Derek
Here is the code.
Java Code:public class MyAnimalList { private Animal[] animals = new Animal[5]; private int nextIndex = 0; public void add(Animal a) { if(nextIndex < animals.length) { animals[nextIndex] = a; System.out.println("Animal added at " + nextIndex); } } } ________________________________________________________________ public class AnimalTestDrive { public static void main (String[] args) { MyAnimalList list = new MyAnimalList(); [B][COLOR="Green"]Dog a = new Dog();//THIS IS NOT DECLARED ANYWHERE! Cat c = new Cat();[/COLOR][/B]//EITHER IS THIS list.add(a); list.add(c); } }
- 05-02-2011, 08:03 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
Here's an example:
You can assume that Cat and Dog are declared somewhere. The declaration isn't that important for your example. The point is that you can treat Cat and Dog objects like Animal objects if they inherit (extend) Animal.Java Code://in Cat.java public class Cat extends Animal { //some code here } //in Dog.java public class Dog extends Animal { //some code here } //in Animal.java public class Animal { //base definition of Animal }
- 05-02-2011, 09:14 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Wow thank you. I had to go over it again even after your explanation. LOL. But after some breadcrumb trail analysis, I understand it. I happened to come across another part of the book I don't understand, a snippet of code. It is on page 213 of "Head First Java". It explains that there is no "bark()" method in the class "Object". But there is an "al.get(index)" and I don't know if al is some kind of keyword or if it is just a sample name they gave to their arrayList in this example. any more help greatly appreciated. thanks for helping. Here is the code.
Java Code:Object o = [B][COLOR="Green"]al[/COLOR][/B].get(index); int i = o.hashCode(); o.bark();// illegal, there is no Object method of "bark".
- 05-02-2011, 09:20 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
In this code al is an array list, you extracted the item from the array list at index. Then you store it as an Object.
- 05-02-2011, 09:23 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
Similar Threads
-
Head First Java book-Abstract methods question
By silverglade in forum New To JavaReplies: 4Last Post: 04-30-2011, 12:45 AM -
Is the Head First Java book wrong about Strings and GC?
By raindog308 in forum New To JavaReplies: 2Last Post: 02-07-2011, 10:05 PM -
Java Project Head, Kochi
By enroutehuma in forum Jobs OfferedReplies: 1Last Post: 11-09-2009, 06:44 PM -
Monitor Disc Head Using Java
By zorroforce in forum New To JavaReplies: 4Last Post: 09-18-2009, 07:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks