Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default Head First Java book-Constructor question

    Hi, I am having trouble understanding about a constructor on page 255 of the "Head First Java" book if anyone has it. But here is the code.

    Java Code:
    public abstract class Animal {
                 private String name;
    
                 public String getName() {
                   return name;
                 }
    
                  public Animal(String theName) { [COLOR="Green"]//WHY DO WE NEED THIS[/COLOR]
                    name = theName;                  [COLOR="Green"]//when we have "return name" [/COLOR]
                  }
               }
    __________________________________________
    
    public class Hippo extends Animal  {
           
             public Hippo(String name)  {
                 super(name);
           }
    }
    
    _____________________________________
    
    public class MakeHippo {
    
             public static void main(String[] args)  {
                 Hippo h = new Hippo("Buffy");
                 System.out.println(h.getName());
             }
    }
    
    _______________________________________

    I don't understand why we need this

    Java Code:
     public Animal(String theName) {  
                    name = theName;                 
                  }
    when we already have this

    Java Code:
     public String getName() {
                   return name;
                 }
    why couldn't we just have this?

    Java Code:
    public String getName(String name) {
                  return name;
              }

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Uhm..? They do two completely different things.

    The constructor creates the Animal and set its name to the value of theName, while getName() just returns the name of an existing Animal.

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

    Default I have another constructor question

    I have another constructor question on page 256 of the "Head First Java" book. Here is the code, I have commmented what I don't understand. Any more help GREATLY appreciated. Thanks. Derek


    Java Code:
    import java.awt.Color;
    class Mini extends Car {
    
          Color color; [COLOR="Green"]//I don't understand what this does.[/COLOR]
    
          public Mini()  {
               this(Color.RED);[COLOR="Green"]//I don't understand what this does[/COLOR]
          }
    
           public Mini(Color c)  {[COLOR="Green"]//nope. don't understand this either.[/COLOR]
                super("Mini");[COLOR="Green"]//what is this call? a constructor in the "Car" class?[/COLOR]
                color = c;
                //more initialization
           }
    
           public Mini(int size)  { [COLOR="Green"]//still don't understand.[/COLOR]
               this(Color.RED);
               super(size);
    Thank you.
    Last edited by silverglade; 05-04-2011 at 04:14 PM.

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

    Default

    thank you ojn. I was wondering also, when I type in

    Java Code:
    Hippo h = new Hippo("Buffy");
    does that call the Animal constructor? please.

  5. #5
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    It's really well explained in the book and I fail to see how we can make it any clearer. You really need to study constructors and how super() and this() work with them more in depth.

    Before looking up what the book actually says I thought about what I could possibly write to make it clearer than what I assumed the book already has. And my explanation would have been pretty much identical to their.

  6. #6
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by silverglade View Post
    thank you ojn. I was wondering also, when I type in

    Java Code:
    Hippo h = new Hippo("Buffy");
    does that call the Animal constructor? please.
    There's an easy way to test this. Create a System.out.println("Animal created"); in the Animal constructor and let your test code do nothing but create a Hippo object as you have above. If it prints "Animal created", then yes, it's called.

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

    Default

    Ok thank you. I will try harder to understand it in the book. Sorry.

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    I would advice you to read Constructor chaining, Constructor Overloading and how to use super and this keywords.

    And this page has it all : Java Constructors

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by silverglade View Post
    Sorry.
    No need to apologize for not understanding. :) There's a lot of stuff I have yet to grasp and I've been working with Java (although not professionally) for many years.

    My point was merely that I don't think many of us can explain this much better than the book, at least not without writing a minor essay. And odds are you wouldn't understand too much of that either as what's covered in that chapter of the book are pretty much the raw basics.

    So by all means, keep asking questions about what you don't understand. It's much better than being quiet. And hopefully one the inveterate pros will note your questions and provide better answers than I can. :)

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

    Default

    Thanks ojn. I just finally understood it on the link goldest gave me. For some reason the linked page explained it better to me than the book. WOW goldest! LOL. How did you find that page so fast, it explained exactly those things that were on page 255 with the Animal and Hippo classes. I UNDERSTAND PAGE 255!! LOL. But I haven't tried to reread page 256 yet. MAN. Learning programming is the HARDEST thing I have ever done in my life. LOL. and this is just the beginning, just learning the syntax. It is strange! The code looks like CHINESE to me, then I understand it and BANG it turns into a type of English that I understand and the gibberish code is readable. That is really a strange experience. I would say programming is way harder than learning a verbal language because there is math and logic. But that BANG understanding experience is the same. Very strange experience, learning to understand code. Thank you for that link. Also, I like the Joker avatar. That was a great movie. The actor for the Joker was Amazing. I heard he died, but I don't know.

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

    Default

    Ok I strained my brain on page 256. I still have questions but I do understand more of it. Here is what I understand and don't understand commented. Any more help GREATLY appreciated! thanks.

    Here is the page code:

    Java Code:
    import java.awt.Color; 
    class Mini extends Car {
    
          Color color; [COLOR="Green"]//This looks like a "Color" variable declared.[/COLOR]
    
          public Mini()  {
               this(Color.RED);[COLOR="DarkRed"]//I don't know what "Color.RED" does. [/COLOR]
          }                          [COLOR="Green"]//I understand the "this" statement [/COLOR]
    
           public Mini(Color c)  { 
                super("Mini");[COLOR="DarkRed"]//what is this call? a constructor in the "Car" class? The quotes around MINI confuse me[/COLOR]
                color = c;
                //more initialization
           }
    
           public Mini(int size)  { [COLOR="Green"]//I understand this, you can't have both in there.[/COLOR]
               this(Color.RED); [COLOR="Green"]  //can't have this and super in one constructor.[/COLOR]
               super(size);

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

    Default

    It first declared a color instance variable for the car. From there it uses the existing constructor to pass in the color red via the this keyword. The car class presumably has a few instance variables like size, name, etc. The one that takes a color supplies a default name for the car.

    Finally, for the use of this and super in one constructor I advise youth compile and run it and see what happens.

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

    Default

    Thank you again Sunde887. That cleared it up more. let me see if I explain what I think it means if I am right? Here is my explanation of what I think it means. please. Based on what you told me above.Thank you. Derek

    Java Code:
    public Mini()  {
                 this(Color.RED);[COLOR="Green"]// passes the color "RED" to the constructor below and calls it with "this"?[/COLOR]
    
              }
    
              public Mini (Color c)  {
                  super("Mini");[COLOR="Green"]// There must be a "name" argument for class Car? So this passes "Mini" as an argument to the "Car" class[/COLOR]
                  color=c;       [COLOR="Green"]//so for this car we called it "Mini"?[/COLOR]
                  //more initialization
    Sorry for not being able to get it yet totally, I am not the best programmer minded. But I WORK HARD. LOL. I will not give up until I learn Java.

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

    Default

    Don't give up. That looks fine. Learning to program takes some time to get used to thinking correctly, once you get your mind thinking about it correctly it gets a lot easier. When you finish head first java, or even if you get ambitious, try reading some of htdp at htdp.org.

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

    Default

    Thank GOD Sunde887, if you said my interpretation was wrong I was going to like jump out my window. LOL. You are right, it is like training my mind to have the ability to understand symbols and "follow the trail" of the code, as it is like following little breadcrumbs along different little paths. I think (I pray) these are the 2 hardest pages in the book, as I got through about 250 pages only needing to ask a few simple questions on the forum but when I got to this it was pretty bad. hehe. As long as I never give up, I will turn out ok. It is just taking longer than I expected, because I am not a "genius", I am of average intelligence. I will check out htdp.org after getting done with the book, thank you! Derek

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

    Default

    Thank God for you and this forum, and the people on it.

  17. #17
    shazam's Avatar
    shazam is offline Member
    Join Date
    Jan 2011
    Location
    USA, CA
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by silverglade View Post
    Thank God for you and this forum, and the people on it.
    I agree! :D

    I'm starting chapter 2 in "Java Head First" and it all still looks like foriegn stuff
    Hope I can be at page 255 some day!

    It just takes me alot longer to learn all this than most people....wish it didnt...but it does :cool:
    Java Newbie

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

    Default

    It'll stick eventually and you will be shocked at how easy a lot of the basics actually are. As you get more into it you will also be able to read and understand a lot more (meaning number of books and code in the books).

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

    Default

    Shazam, Yes it will get easier, some parts, very small, I had problems with like constructors, but the rest wasn't bad, you just have to bang through those walls then surf, and definitely study with a pen or pencil so you can make little diagrams and arrows that show the code flow to help your eye bounce around the code easier for later review. I always study with a pen, I find it impossible to learn material without a pen handy to write notes right in the book, in my own words, or even the forum advisors' words. Good luck fellow Newbie!!

  20. #20
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Silverglade,

    I am really happy that you went through that link and it actually helped you to understand the concepts.

    Keep going with your journey and keep coming back here whenever you face any problems. People here on this forum would be more than happy to help you out.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Page 1 of 2 12 LastLast

Similar Threads

  1. Head First Java Book question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 05-02-2011, 09:23 PM
  2. Head First Java book-Abstract methods question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 12:45 AM
  3. 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
  4. Java Constructor Question
    By ashly in forum New To Java
    Replies: 6
    Last Post: 03-09-2010, 02:10 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
  •