|
Re: Java Object Question [message #892268 is a reply to message #892263] |
Wed, 27 June 2012 14:32 |
|
On 6/27/2012 8:17 AM, Alan Farroll wrote:
> Hello,
>
> I am fairly new to both Java and Eclipse and just completed a tutorial
> named "Eclipse and Java for Total Beginners". I am also learning more
> about Object Oriented programming. I am confused about one process in
> the tutorial and wondered if someone could tell me how this occurs.
>
> I don't want to publish all the code so in brief, I have a class of
> Books and a MyLibrary class.
>
> In BOTH the Book class and the MyLibrary class the tutorial has us
> create a toString method to override the Java toString method, created as;
>
> public String toString(){
> // method expressions
> }
>
> In the MyLibrary class I have an ArrayList of Books and a method to
> print out the status of each book using the expressions
>
> for (Book thisBook : this.getBooks()){
> System.out.println(thisBook);
> }
>
> At the line, "System.out.println(thisBook) " above the program jumps to
> the toString method in the Book class and processes the expressions.
> Why does this occur? And why does it go to the toString method in the
> Book class and NOT the toString method in the MyLibrary class?
>
> Thanks in advance
>
> Alan
You're asking to println() an object of class Book; does it not make
perfect sense that Book's toString() method be used to express a book in
a String way for output?
Java questions are answered in forums like javaranch.com,
stackoverflow.com, jguru.com, etc. Eclipse forums answer questions on
setting up and using Eclipse.
If you're just getting started, but you're committed, I'd suggest the
following books (I'm not paid to say this, it's just my opinion):
- Java Head First (use as a fun method to learn)
- Thinking in Java (use as a grammar to look things up in)
Best of luck.
|
|
|
|
Powered by
FUDForum. Page generated in 0.07971 seconds