next up previous
Next: Specification vs. Implementation Up: Week 6 Previous: Week 6


toString() Method

Would you believe that every class has a method called toString() which returns a reference to a String? And when we say every class, we mean exactly that: even the classes that you create yourself will have a toString() method. Even if you don't write a toString() method for your class. How does this happen?

It turns out that every class in Java, even the ones you create yourself, is a 'child' of some other class. And being a 'child', the class inherits certain attributes from its parent. In fact, although we won't talk too much about this right now, this mechanism is called inheritance; if you're interested in knowing more about it, you can look it up in your text or in one of the many Java tutorials on the web. All we need to know right now is that when we create a new class, it is a 'child' of the most basic class in Java: the Object class. Not only can the Object class have children, but its children can have children (small lie - not all classes can have children, but close enough to the truth for now) and so on. In some sense, the Object class is the 'grandmother of all classes'; it has no parent.

At any rate, inheritance allows a 'child' class to automatically have a method definition that its parent has. This is how every class has a toString() method: since Object has a toString() method, then 'children' of Object inherit a toString() method, the children of children of Object inherit a toString() method, and so on. So every class 'automatically' gets a toString() method by inheritance.

But what does this toString() method actually do? The toString() method is intended to create a string which describes the object. By default, toString() creates and returns a reference to a String which looks like:


ClassName@4edf23fa6cc3

How does this 'describe' the object? The answer is that it doesn't do a very good job. It prints the name of the class that the object came from, and some other alphabetic and numeric characters related to where the object is stored in memory, but nothing else. If we've written a class and just inherited the method from a 'parent' class, there's no reason why it should be well suited to the 'child' class: 'parents' don't understand their 'children' very well.

But we can fix that situation. We can write our own toString() method, and have it create and return a reference to a string of our choosing. This way, we can customize the toString() method so it is well suited to the objects of our 'child' class. Can we do this? Can we write a method with the same name, with the same parameters (in this case none), and the same return type as a method is inherited? The answer is yes: doing this is called overriding the inherited method, and if the method that we write has the same name, parameters, and return value, then our method will be called rather than the one that we inherited.

Therefore, when we want to write a method that returns a reference to a string which better represents an object in our class we just write a toString() method. For example, a toString() method for our Student class might look like this:


public String toString(){
        return name;
}

Or perhaps like this:


public String toString(){ 
        return name + age;
}

We can define our toString() method as we like, as long as it has no parameters, returns something of type String, and has the name toString.


next up previous
Next: Specification vs. Implementation Up: Week 6 Previous: Week 6
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09