• Post Reply Bookmark Topic Watch Topic
  • New Topic

I don't understand what these two methods do.  RSS feed

 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



Above, I don't understand what get getObject() does.

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



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
 
Emmanuel de Ternay
Greenhorn
Posts: 5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just look at line 3 of the first method: it calls the 2nd method with getObject(aDog), passing a Dog object as argument.

The second method returns an Object (not a Dog, but an Object), so line 3 of the first method won't compile, because it tries to put an Object in a Dog variable (compiler knows that method returns an Object, but doesn't know the exact type). You must cast the result of the method, like this:

Hope this will help you understand the code.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great thank you very much for that! Also, is "Object" and "getObject()" part of the java language or did they make that up please? thanks. Derek EDIT: NEvermind I get it thank you Emmanuel.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an Object class in Java. In fact...EVERY object is a subclass of Object, either directly or because it's super class is a direct subclass of it, or it's superclass superclass is a direct subclass...(and so on).
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Emmanuel de Ternay and thank you for noticing that question which the rest of us seem to have overlooked.

Object is a class in the standard Java™ API, and getObject is a method Sierra and Bates wrote for the book.
 
Emmanuel de Ternay
Greenhorn
Posts: 5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything in Java is an "Object". When you write code for a class, implicitly, this class derives from Object. If your class derives from another class, then that class derives from Object (or another class that extends Object, but at the end, all classes derive from the Object class in java). Object is part of java language.
getObject() is not part of java language, it is a method declared in the code by the programmer and that you can use in other parts of the program.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
derek, please BeForthrightWhenCrossPostingToOtherSites.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone for helping me. I understand. Understanding code is like on those spanish holidays when you bust open those pinatas and candy comes flying out. It is frustration, then a mental BANG> hahaha. thank you!! Derek
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing the getObject() does is confusing the compiler, it returns the same object you pass as a parameter only it upcasts it into an Object type.
So

Those two objects are actually the same object as the output will show.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, ya the people at the Java Forums got mad that I cross posted too. I didn't even know that was wrong. But next time if I don't get an answer at one for an extended period of time, I will try another. Sorry about that. I won't do it at the same time again, I got desperate. And I didn't even know what cross posting is. Ok sorry. Derek
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't care if you DO cross-post, we just want you to tell us. Nothing is more annoying than taking time out to answer a post, only to find that you have already gotten the answer somewhere else. it feels like a great big waste of time. If you say "Hey, I'm posting this same question HERE as well", then before I answer you here, I can check to see if you've gotten your answer there.

Note that other sites may take a different view on the matter.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh I get it, I will let people here know if I cross posted, but I think it is best if I give one forum a chance to answer first, so I will maybe post a question to one, wait a while, then if no answer, tell everyone that I am cross posting. Sorry. Derek
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!