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()?
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.
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
Joined: May 08, 2005
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?
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."
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?
Joined: May 08, 2005
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?
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.
Joined: Feb 08, 2004
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
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.