This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Whilst working through a Java tutorial I noticed that the File class has a default method i.e. File myFile = new File("c:/dir1/myfile.txt"); System.out.println("My file is " + myFile); displays My file is c:\dir1\myfile.txt Presumably toString() is the "default" method for the File class. Q1. Do all classes have "default" methods? Q2. As all class inherit from Object is toString() always the "default" method? Q3. If not how do you specify a "default" method. Mike Dennison slightly confused in Woodford Green
Hi Mike, Welcome to JavaRanch. All java classes have a default toString() method since they inherit it from the Object class. In the case of the File class though, toString() is not the default version but an overloaded version defined in the File class. Try this:
Here's the output:
Note that the Object version of toString() prints the class and the memory position for the object reference.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
... and, toString() isn't really a "default" method. The definition of the "+" operator when used for String concatentation specifies that a class' toString() method be called to convert the operand to a string so that it can be operated upon. Outside of this operation, there is no such concept of a "default" method. hth, bear
... and, toString() isn't really a "default" method. The definition of the "+" operator when used for String concatentation specifies that a class' toString() method be called to convert the operand to a string so that it can be operated upon. Outside of this operation, there is no such concept of a "default" method. More specifically, a StringBuffer is created to concatenate the Strings by calling the appropriate append() method. To spare any confusion to someone making their first post in the Beginner forum, I saw no point in getting into a discussion on semantics. In my mind the question clearly dealt with polymorphism regarding inherited methods. But I would also argue that in some contexts the term default is used to describe a method defined in a super class that may be overridden. For example, all of the abstract Adapter classes in the java.awt.event package have empty default implementations for each event type so that subclasses can override only those events that they are interested in.
So what is in fact happening is that the compiler translates System.out.println("My file is " + myFile); into System.out.println(new StringBuffer().append("My file is ").append(myFile).toString()); The append method as overloaded for Objects (which gets called with myFile as parameter) calls String.valueOf(myFile), which in turn calls myFile.toString() if myFile is not null.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jun 01, 2003
Wow! I'm impressed - so many excellent replies - so quickly. That really helps me a lot. If I may ask one further question. Ilja mentioned how the java compiler processes the "+" operator in println statements, where's the best place to read up on this sort of stuff? Mike
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com