Results 1 to 5 of 5
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default 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);
    
                     }
               }

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    Here's an example:
    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
    }
    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.

  3. #3
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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".

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Ok thank you both for helping. sunde887 I guess I thought al was some kind of Java class or something. I don't know . LOL. I still have to evolve my brain yet to finish this book. Then i will be superhuman! hehe. thanks. derek

Similar Threads

  1. Head First Java book-Abstract methods question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 12:45 AM
  2. Is the Head First Java book wrong about Strings and GC?
    By raindog308 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 10:05 PM
  3. Java Project Head, Kochi
    By enroutehuma in forum Jobs Offered
    Replies: 1
    Last Post: 11-09-2009, 06:44 PM
  4. Monitor Disc Head Using Java
    By zorroforce in forum New To Java
    Replies: 4
    Last Post: 09-18-2009, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •