This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

methods confusion

 
Jolie Lee
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got some questions about methods.. let's take the class below as an eg:

public class Time1 extends Object
{
public Time1()
{
}

blah blah blah...

public String toString()
{
}
}

ok, my question is this, i understand that toString is one of the method in class Object. Since now i've extend class Object, why can't i just call the method toString? why i need to write another method in my class Time1()?

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jolie Lee:
... Since now i've extend class Object, why can't i just call the method toString? why i need to write another method in my class Time1()?

You certainly could just call the method toString() as defined in Object, but the returned value might not be what you want for your new class. Overridding the method allows you to customize it for your needs.

(Note that all classes automatically extend Object. You do not need to explicitly specify this.)
 
Jolie Lee
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ic, i understand that for some programming language we are not allowed to redefine the build in functions, that's y when i see the overidding of method toString, i was confused.

is there anywhere i can see the code of class object? how am i suppose to know that the returned value of class Object 's toString is not what i want?
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can print it:
System.out.println(MyClass.toString());
 
Sanju Thomas
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, You can find the source code of all java classes in the JAVA_HOME/src.zip file.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the source code mentioned by Sanju, the API should provide the info you need...

http://java.sun.com/j2se/1.4.2/docs/api/index.html

The following information is under the toString() method in the Object class...

"The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character '@', and the unsigned hexadecimal representation of the hash code of the object."

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile and run this...
 
Jolie Lee
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:
you can print it:
System.out.println(MyClass.toString());


i've tried this and it gives me cannot resolve symbol error..

is MyClass an instance?
 
Jolie Lee
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Compile and run this...



i've compiled and run and i got this as output:
1: TestOne@923e30
2: I am a customized String.

i understand what u are trying to explain to me, but i got another question, which is.. my class TestOne is empty, but why it'll have TestOne@923e30 as output? shoudln't it be empty as my class is empty?
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may not have done anything with it, but when you created an instance of it, an object was created somewhere in the heap. That is (roughly) the default information that .toString() diplays. This is also why you generally override the .toString() method, as it rarely displays anything useful.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i got another question, which is.. my class TestOne is empty, but why it'll have TestOne@923e30 as output? shoudln't it be empty as my class is empty?

i guess you just didnt pay atention to marc weber's post
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jolie Lee:
... my class TestOne is empty, but why it'll have TestOne@923e30 as output? shoudln't it be empty as my class is empty?

Please review the above posts...

All classes automatically extend (inherit from) _______, in which toString() is defined to return ________________.
[ May 10, 2005: Message edited by: marc weber ]
 
Jolie Lee
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i understand that all class automatically inherits from class Object...

but there are so many methods in class Object, it will automatically call all the methods? or just toString?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a class extends another class, it inherits all non-private members of that class.

Also, consider that instances of a subclass are also instances of the superclass. So because MyClass (any class I define) implicitly extends Object, instances of MyClass are also instances of Object.

The following code has some methods that are probably unfamiliar to you, but the output should be informative. First, it demonstrates the inheritance of two more methods in Object -- hashCode() and getClass() -- which are then used to show the default behavior of toString(). Second, it uses the instanceof operator to show that an instance of a subclass is also an instance of the superclass (Object). And third, it lists all of the public methods in the subclass, which in this case are all inherited from Object.

You might want to read the "Classes and Inheritance" section in Sun's Java Tutorial...
http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

And the "Reusing Classes" chapter in Bruce Eckel's Thinking in Java...
http://www.faqs.org/docs/think_java/TIJ308.htm
[ May 11, 2005: Message edited by: marc weber ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic