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

    Default Question about the meaning of some code.

    Hi. I have a question about some code from my book "Head First Java". The first method of the book, it says it is illegal. The second method the book says is legal, but I don't know how it relates to anything, especially to the first method above it. here are the two methods. (I tried to find getObject in the java documentation, but I can't find it)

    method one:

    Java Code:
    public void go() {
                  Dog aDog = new Dog();
                  Dog sameDog = getObject(aDog);
              }
    Above, I don't understand what get getObject() does.

    and here is method two, which I really don't understand in any way. LOL.

    Java Code:
    public Object getObject(Object o) {
                   return o;
              }
    I don't know how method two relates to method one as it doesn't mention anything about a Dog object. any help GREATLY appreciated. Thank you. Derek:D
    Last edited by silverglade; 04-29-2011 at 07:40 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    I think those are just examples, and they don't necessarily have anything to do with one another. The first example is showing you that you can't use getObject() unless it's defined somewhere. The second example is just showing you what a method like that might look like.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default

    Would you mind telling me what page it is on so I can see in what context the example is being given?

    Also, it's probably as kevin said, and just an example.

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

    Default

    ok thank you. The methods are on page 212 of the book. thanks. Derek EDIT: OH NOW I GET IT. LOL. OK thank you for explaining that. I understand now. The second one was just hypothetical. I understand the first one too now. Thanks. Derek

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    This is an example of how it is legal to 'upcast' from subclass to superclass, but not legal to 'downcast' from superclass to subclass.

    You can pass a Dog to the getObject method which expects an Object, because a Dog is a subclass of Object. All classes are subclasses of Object, so they can be used wherever Object is expected. This is called 'upcasting' - the subclass type is automatically cast up to the superclass type.

    However, the getObject method returns an Object and the go method tries to assign the returned Object back to a Dog. This won't work because although any class instance is a kind of Object, the reverse is not true - an Object is not necessarily a Dog. This cast can't be done automatically, so the code won't compile; if you really want it, you have to explicitly tell the compiler what subclass you want the superclass cast down to. This is known as 'downcasting'. If you get it wrong, your code will crash.

    In general, downcasting should be avoided, and needing to use it is probably a sign of poor design (except in certain specific circumstances).

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

    Default

    I read the page, I see that you understand it, however; it is meant to show you that you can't assign the Dog to something that returns an object. The getObject method returns an object, even though you know it's actually a dog, the compiler doesn't. Pay attention to how the second example differs in declaring sameDog.

    bad looks like this
    Java Code:
    Dog sameDog = getObject(aDog); //here you are assigning an object to a dog
    If this was allowed you would run into big problems. Everything is an object, so if this was possible, you would be allowed to assign anything to anything, you would have cats that are dogs, and other chaos!

    here is the good
    Java Code:
    Object sameDog = getObject(aDog);
    In this one you are assigning it to an object reference so it's kosher.

    Alternatively, you should be able to do
    Java Code:
    Dog sameDog = (Dog)getObject(aDog);
    This uses a cast to cast the object returned by getObject to a dog. This works since we know it is a dog disguised as an object.

    I hope this has helped you fully understand. Also, if you run into more stuff that you are having trouble with, feel free to supply the page as well as your problem. I am here a lot, and I have quick access to head first java most of the time.
    Last edited by sunde887; 04-29-2011 at 07:59 PM.

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

    Default

    Thank you for that! I just have a final question. 2. Is getObject() part of the java language or did they make that up please? and is the word "Object" that is from "Object sameDog" part of the java language as well? any more help greatly appreciated. Thanks everyone for helping and thanks for offering to help me with my book Sunde887!D:D

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

    Default

    getObject is just some method they defined to be used in the example. However; Object is like the super class of java. Every class extends Object. You can think of the following class declaration
    Java Code:
    class X{}
    to be the same as
    Java Code:
    class X extends Object{}
    of course, you never have to extend it yourself, that's done by java for you.

    All classes extend object so they all have some common functionality. equals, hashcode, toString are a few of the methods.

    For more, check this out : Object class API

    I loved that head first java book, it helped me a lot in understanding the language. I hope you are enjoying it as well.

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

    Default

    you are awesome sunde887 haha! I understand it!. THANK YOU! Jeez! Understanding programming code when you spend like one hour or more on ONE snippet is like BUSTING OPEN a Pinata. Those animals you bust open as a spanish holiday and candy comes out. LOL. It's like a mental BANG. LOL. I love the Head First book,

    yes I am enjoying their great examples, notations and humor. It is the best programming book I have ever bought. It is just that sometimes, like with the second sink the dotcom's game program, they went way over my head, but other than that, it's great. Here is another snippet of code I don't understand, don't know if you know it but I bet you will. It is on the next page, page 213. Here is the code, I have no idea its Greek.


    Java Code:
    Object o = al.get(index);
              int i = o.hashCode();
              o.bark();
    They say that o.bark() won't compile. But al.get(index);, I have know clue what al is. LOL. and I forgot what a hashcode is I think it is a number value of the object? please. thank you . Derek:o

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

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

    Default

    what is cross posted mean? I post in 2 forums because if I don't get an answer in one, I might get an answer in the other. Is that wrong:?

  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

    I am guessing in the second sink the dotcom example you had a hard time understanding there algorithm, and you should at the time. Like they say, that can, for the most part just be ignored, understanding the rest is good.

    al in this case is just an ArrayList<Object> I am not sure they went over generic parameters yet. I believe they do a fairly good job explaining them at some point. It basically says, everything in this array list is an Object, and will come out as an Object. the al.get method returns a reference to some object on the heap. Since the array list is parametrized by <Object> it will return an item of class Object.

    So you are assigning the reference o to the object at the supplied index of the arraylist.

    Now that you have extracted the item and stored it in some reference you get the hashCode. hashCode is a method that all classes inherit from Object, it returns a semi-unique number used to allow quick lookup in hashes. This may not be covered in this book, and if it is, probably not in too much detail. Don't worry about the exact details of what a hashcode is for yet, just remember it's a semi-unique identifier. Notice I say semi-unique, and not unique. It's not common that a lot of objects will have the same hashCode, but it is absolutely possible. Only the absolute best algorithm would produce 100% unique hashcodes. I also don't think there is an algorithm so good that it returns absolutely unique numbers.

    Finally, it tries to call bark on the Object referenced by o. It's strange because you and I both know that o is in fact a Dog, however; the compiler doesn't. Similar to the last exmple, if this was allowed you would have lions, and birds barking. If you wanted, you could down cast and allow the bark with this
    Java Code:
    Dog d = (Dog) o;
    d.bark();
    but as Diorde said, this should be avoided in most cases, since it may not always be a dog being extracted from the array list and it would cause errors to cast a non dog to a dog.


    Finally, I know exactly what you mean when you said it's like busting open a pinata. Another book that I recommend very strongly is htdp, it's freely available at htdp.org, and it uses a dialect of lisp called dr.Scheme, it has many truly challenging exercises, and really teaches you well. It uses recursion extensively.

    If you are comfortable with reading two books at once I greatly suggest this book as well.

    When I was reading this book there was one exercise that I had an exceptionally hard time with, it was called Missionaries and cannibals, the goal was to create a recursive AI search to find the quickest solution to the missionaries and cannibals problem. It took me 3-4 days stuck on a small bit, when I finally figured it out and got it to print out all the intermediate steps it felt amazing.

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

    Default

    I am not 100% sure about whether it's allowed here or not, however; a big problem with cross posting is that people will post the same question on 3-4 forums and then find an answer on one and ignore the other posts which also had valid answers. It's frustrating for the people helping you to spend time understanding your problem only to have you never return. DB may post you some links with more details on the problems with cross posting.

    I'd like to say, that for the most part, if you are having troubles with head first java, I can usually help you out. I spend a ridiculous amount of time on these forums as well, and if I can't help there are people here who are much smarter than I, who can help you.

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

    Default

    You are right. I should have waited a while to get an answer here before going to javaranch. I made it a point not to ignore anyone's answers to me. But you are right it is better to do one forum first, then if that doesn't work, do the other, but not at the same time as that is not really fair to everyone. Sorry about that, I got so frustrated and impatient that I posted on both at the same time. LOL. I won't do that again. Sunde887 you are awesome for writing all of that. I hope you make a million bucks as a programmer.

    I didn't read your previous post yet but I am going to read it now. Sorry about the cross post I won't do that anymore, unless I failed to get an answer from one, then I go to the other later. Thank you! Dereko)
    Last edited by silverglade; 04-29-2011 at 09:17 PM.

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

    Default

    I forgot to mention, I will post my questions on another forum only if my first one fails, after a while of waiting, but even then I will tell everyone at the forums that I am "cross posting" so no one gets mad and they can see if my question was already answered. Sorry. Derek

Similar Threads

  1. Phrase Meaning
    By griddle in forum New To Java
    Replies: 1
    Last Post: 04-25-2011, 07:45 PM
  2. What is the meaning of this statement in the book?
    By hitesh_public in forum New To Java
    Replies: 2
    Last Post: 10-30-2010, 04:42 PM
  3. Explain the meaning of each word
    By saichandra in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 05:28 PM
  4. for loop meaning
    By ahmakki in forum New To Java
    Replies: 3
    Last Post: 04-12-2010, 02:06 AM
  5. Exception:Meaning
    By tiger100plus in forum New To Java
    Replies: 2
    Last Post: 11-27-2008, 08:45 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
  •