aspose file tools*
The moose likes Beginning Java and the fly likes methods confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "methods confusion" Watch "methods confusion" New topic
Author

methods confusion

Jolie Lee
Ranch Hand

Joined: May 08, 2005
Posts: 87
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.


Jys<br /><a href="http://jy-s.com" target="_blank" rel="nofollow">http://jy-s.com</a><br /> <br />Trying my very best to learn java, please forgive me if i'm asking some really stupid questions.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jolie Lee
Ranch Hand

Joined: May 08, 2005
Posts: 87
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

Joined: Feb 08, 2004
Posts: 1281
you can print it:
System.out.println(MyClass.toString());


java amateur
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Yes, You can find the source code of all java classes in the JAVA_HOME/src.zip file.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

Compile and run this...
Jolie Lee
Ranch Hand

Joined: May 08, 2005
Posts: 87
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

Joined: May 08, 2005
Posts: 87
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

Joined: Aug 20, 2001
Posts: 1824

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: May 08, 2005
Posts: 87
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

Joined: Aug 31, 2004
Posts: 11343

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 ]
 
 
subject: methods confusion