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-Abstract methods question

    Hi, On page 203 or my "Head First Java" book, they talk about abstract methods. I understand the purpose for Abstract classes, you don't want to be able to make an object of an abstract class like ""Animals", that is an abstract term. Something more concrete like "Dog" you could make an object out of.

    However, I have no idea what an abstract method is for if it doesn't have a body. So it has in the book this example.

    Java Code:
    public abstract void eat();
    and that is IT!! NO body? what the heck? LOL. if any one can, please help me understand what abstract methods are for please. Any help greatly appreciated. Thanks. Derek:D

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

    Default

    Abstract methods are similar to abstract classes. The method doesn't need a body, just a prototype(public abstract void eat(); ). The prototype tells you that everything that inherits it from animal needs to implement the method.

    So if you were to make the following class
    Java Code:
    class Dog extends Animal{}
    it would have to have a definition for eat. I don't know if you have learned interfaces yet, but they are similar. They are like a fully abstract class, none of the methods require bodies, and if you implement an interface you must also implement each method that the interface declares.

    The example in the book is using this because you don't want a frog, spider, dog, and elephant to eat the same, so you leave it to the person extending from the super class to implement the proper way to eat. This also allows you to have different classes have different ways of doing something.

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

    Default

    Thank you so much sunde887. You are my savior, no joke. If not for your replies I wouldn't be learning, and not learning java=stay at low paying job for life. LOL. ok let's see if I understand you in the following code?

    Java Code:
    public abstract void Animals(){
              public abstract void eat();
              }
    and then to extend the Animals class and implement the eat() method maybe?

    Java Code:
    class Dog extends Animals() {
              public abstract void eat() {
               int dogFoodPieces = 0;
               dogFoodPieces++;
               System.out.println("your dog  has eaten". dogFoodPieces. "pieces of Dog Food.");
               }
            }
    I don't know just stretching my tiny brain here. LOL. any more advice greatly appreciated. youre the GURU!! thanks. DerekDD

  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

    You are close, however animal is a class, and it will look like this
    Java Code:
    public abstract class Animal{
      public abstract void eat();
    }
    
    public class Dog extends Animal{
      public void eat(){
        //do stuff
      }
    }
    You want to treat the concrete implementation as a regular method and not an abstract method.

    I hope this clears it up a little more.

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

    Default

    Great sunde887, that's perfect. Once again , BANG with the pinata., actually was more like, banging my head against the book until my head exploded, and then I saw your code and put my brain back together. HHAHA. Now I am on page 214 of Head First Java, and pretty much understand everything I have read to this point thanks to you. So I will just keep plodding along through the sand, and try not to be lazy and actually READ it instead of just looking at the code and 1950's style pictures. LOL. thank you for the code, I totally understand it , and the moment you go from ignorance to enlightenment is really like singing angels opening up in the clouds and they sing "AHHHHHH!" haha. Learning programming is very interesting psychologically, it is a series of discomfort and pleasure back and forth, but I haven't even got to the really fun part yet which is creating real programs. thanks . Derek

Similar Threads

  1. 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, 11:05 PM
  2. Overloading abstract methods??!?
    By tfitz666 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 10:45 AM
  3. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:36 PM

Posting Permissions

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